【问题标题】:.Net Web Request - Unable to connect to the remote server.Net Web 请求 - 无法连接到远程服务器
【发布时间】:2010-09-07 20:58:18
【问题描述】:

我正在使用 .Net Web Request 来读取所选网页的 html。

偶尔会出现以下错误:

2010-09-05 13:14:51,986 [File_29] ERROR Boxer.Classes.GetLinks [(null)] - HtmlUtils - GetResponse - Url - http://domain.com/index.html
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 69.212.110.118:5068
   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, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at Boxer.Classes.GetLinks.GetResponse(String url, Int32 timeout, Boolean& timeoutException)

我确定目标 URL 可用。

知道错误的原因是什么吗?

【问题讨论】:

    标签: c# .net httpwebrequest


    【解决方案1】:

    发生这种情况可能有不同的原因。也许目标服务器正在限制响应,因为它认为请求来自机器人。在这种情况下,您可能会考虑发送正确的 HTTP 标头(如果它在您的浏览器中有效,请尝试发送相同的请求标头)。此外,您还可能需要配置和使用代理。

    【讨论】:

    • 感谢您的回答。我正在使用以下 UserAgent “Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.1”。该错误仅偶尔发生一次,所以我真的不认为它是 UA。关于代理您的意思是什么配置?跨度>
    • 是否需要使用代理服务器才能访问网址?
    • 您是否已将WebRequest 配置为使用此代理?
    • 是的,我做到了。 "webRequest.Proxy = new WebProxy(address)" ,正如我所说的,大多数网络请求我都能完美地工作。它只是得到上述错误的几个。
    • 那么看来问题与服务器有关。