首页 新闻 赞助 找找看

C#后端代码抓取页面偶发性与主机连接失败

0
悬赏园豆:30 [待解决问题]

用 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:连接尝试失败,因为被连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应

只是一个路人的主页 只是一个路人 | 初学一级 | 园豆:139
提问于:2019-11-08 14:28
< >
分享
所有回答(4)
0

服务器不稳定吧~~ 你ping 一下看看。

永远跟党走i | 园豆:1517 (小虾三级) | 2019-11-08 20:55

添加了白名单,不允许ping

支持(0) 反对(0) 只是一个路人 | 园豆:139 (初学一级) | 2019-11-14 11:17
0

==StatusCode=0

通常原因就是网络不通或者不存在该端口服务

花飘水流兮 | 园豆:13560 (专家六级) | 2019-11-08 21:42

正常浏览这个服务器的页面是可以浏览的,但是使用 HttpWebRequest.GetResponse() 抓取页面就会出现无法连接服务器的情况,而且是一点出现取法连接是接下来十几分钟内就都是无法连接状态,我在想的是会不会哪里堵了,下面是报错信息:
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
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()

支持(0) 反对(0) 只是一个路人 | 园豆:139 (初学一级) | 2019-11-14 11:22
0

接口请求量大吗,是不是超出连接数限制了

龙葛格 | 园豆:782 (小虾三级) | 2019-11-14 18:34

访问量不到,有时候本地也会无法连接服务器,连接数设置 512

支持(0) 反对(0) 只是一个路人 | 园豆:139 (初学一级) | 2019-11-19 10:00
0

尝试使用 RestSharp呢?

小小高 | 园豆:1095 (小虾三级) | 2019-11-19 17:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册