首页 > > > C#后端代码抓取页面偶发性与主机连接失败
用 HttpWebResponse 抓取页面时链接主机失败,这种情况是偶然发生,网站自己用的按说并发性不会很高,有没有大侠知道什么原因嘛?
抓取代码:
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
String str = sr.ReadToEnd();
String appPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.TrimEnd(Path.DirectorySeparatorChar);
sr.Close();
英文原文报错 :
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
下面是翻译过来的:
System.Net.Sockets。SocketException:连接尝试失败,因为被连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4741/viewspace-3124634/ ,如需转载,请注明出处,否则将追究法律责任。