.NET 中的 HttpWebRequest NameResolutionFailure 异常(Ubuntu 上使用 Mono)
我有一个通过 Mono 2.10 在 Ubuntu 上运行的 .NET 程序
该程序每分钟左右通过 HttpWebRequest 下载一个网页,大多数情况下工作正常:
String result;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
using (objResponse = objRequest.GetResponse())
{
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
}
问题是几天后我开始抛出异常:
DateTime: 01/25/2012 08:15:41
Type: System.Net.WebException
Error: Error: NameResolutionFailure
Stack:
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
at socks_server.Program.readHtmlPage (System.String url) [0x00000] in <filename unknown>:0
at socks_server.Program.getAccessKeysProc () [0x00000] in <filename unknown>:0
服务器仍然是 abel例如,解析 DNS
wget http://www.google.com
将返回文件,而不会出现任何问题,就像 ping 和解析 DNS 的其他命令一样。
然而,我的程序将继续抛出该异常,直到我重新启动它。重新启动应用程序后,它将再次开始正常工作。
我检查了系统上的打开文件计数(400 左右)、内存使用情况(4GB 中的 327mb)、CPU 使用情况(2-3%),一切都正常。
有什么想法吗?
I have a .NET program running on Ubuntu via Mono 2.10
The program downloads a webpage via an HttpWebRequest every minute or so which works fine most of the time:
String result;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
using (objResponse = objRequest.GetResponse())
{
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
}
The problem is that after few days I start getting exceptions thrown:
DateTime: 01/25/2012 08:15:41
Type: System.Net.WebException
Error: Error: NameResolutionFailure
Stack:
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
at socks_server.Program.readHtmlPage (System.String url) [0x00000] in <filename unknown>:0
at socks_server.Program.getAccessKeysProc () [0x00000] in <filename unknown>:0
The server is still abel to resolve DNS, for example
wget http://www.google.com
Will return the file without any probelm as will ping and other commands that resolve DNS.
My program however will continue to throw that exception until I restart it. After restarting the application it will start working again as it should.
I have checked open file counts on the system (400 ish), memory usage (327mb of 4gb), CPU usage (2-3%) and all are OK.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过将主机名转换为 ip 并将主机名添加到
Headers
集合或Host
属性来解决此问题。如果您的网址是
http://example.com/uri
。楼主自行解决吧。假设它是1.2.3.4
。它将是http://1.2.3.4/uri
。现在将Host: example.com
标头添加到您的请求中。我认为可以通过设置 HttpWebRequest.Host 属性来完成。You can solve it by translating the host name to ip and add the host name to
Headers
collection or toHost
property.If your url is
http://example.com/uri
. Resolve the host yourself. Suppose its1.2.3.4
. It'll behttp://1.2.3.4/uri
. Now addHost: example.com
header to your request. I think it can be done by settingHttpWebRequest.Host
property.我知道这是一篇旧帖子,但面临着同样的错误,所以想分享解决方案。
已连接,只是为了重试我的服务器调用,并稍微休眠一下
之间。它在大多数情况下都有效,否则如果第二次调用
失败我取消请求。
信号非常低。如果没有互联网也会出现同样的错误
即使连接到 Wifi,也无法连接。
这与我的回答一致:
I know this is an old post, but was facing the same error, so thought to share the solution.
is connected, is just to retry my server call with a slight sleep in
between. It works most of the time, otherwise if the second call
fails I cancel the request.
signal is very low. The same error occurs if there is no internet
connection at all, even if connected to Wifi.
This is in line with my ans on :
好吧,我使用 HttpClient - 但这可能是一个类似的问题。我在 Android 设备上遇到了同样的问题(它在 Windows Phone 上工作)...但是在我将主机添加到标头后,它工作了!
client.DefaultRequestHeaders.Host = "mydomain.com";
您仍然可以使用 url 中的名称(不必使用 IP 地址)
Well I use the HttpClient - but it might be a similar problem. I had the same issue on a Android device (it worked on a Windows Phone)... But after I added the Host to the header it worked!
client.DefaultRequestHeaders.Host = "mydomain.com";
You can still use the name in the url (you don't have to use the IP address)
我在 raspbian 上的单声道应用程序中遇到了同样的问题。我尝试过此线程和其他线程中描述的不同解决方案,但没有一个有效。最终,我通过将 /etc/resolv.conf 中的名称服务器更改为 google 的名称服务器来解决问题 https://developers.google.com/speed/public-dns/
Mirko
I was experiencing the same issue in my mono application on raspbian. I've tried different solutions described in this and other threads but none worked. Eventually, I was able to fix the problem by changing the name servers in /etc/resolv.conf to the google ones https://developers.google.com/speed/public-dns/
Mirko
当我在没有互联网连接的情况下启动移动应用程序(Android 或 iOS 并不重要)时,我收到此错误。恢复连接后,每个请求都会返回“NameResolutionFailure 异常”。我不得不等待 120 秒才能让 http 请求再次工作。在应用程序启动的任何位置设置以下代码行,错误终于消失了。
默认 DnsRefreshTimeout 值为 120 秒。
I was getting this error when I started the mobile app (android or iOS it does not matter) without internet connection. After restored the connection every request returns "NameResolutionFailure exception". I had to wait 120 seconds for having the http request working again. Setting the following line of code anywhere in the app startup the error was finally gone.
The default DnsRefreshTimeout value is 120 seconds.