C# .NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8999616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:31:09  来源:igfitidea点击:

HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)

c#ubuntumonohttpwebrequest

提问by antfx

I have a .NET program running on Ubuntu via Mono 2.10

我有一个通过 Mono 2.10 在 Ubuntu 上运行的 .NET 程序

The program downloads a webpage via an HttpWebRequest every minute or so which works fine most of the time:

该程序每分钟左右通过 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();
            }
        }

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

服务器仍然可以解析DNS,例如

 wget http://www.google.com

Will return the file without any probelm as will ping and other commands that resolve DNS.

将返回没有任何问题的文件,ping 和其他解析 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.

我检查了系统上的打开文件数(400 ish)、内存使用情况(327mb of 4gb)、CPU 使用情况(2-3%),一切正常。

Any ideas?

有任何想法吗?

回答by Shiplu Mokaddim

You can solve it by translating the host name to ip and add the host name to Headerscollection or to Hostproperty.

您可以通过将主机名转换为 ip 并将主机名添加到Headers集合或Host属性来解决它。

If your url is http://example.com/uri. Resolve the host yourself. Suppose its 1.2.3.4. It'll be http://1.2.3.4/uri. Now add Host: example.comheader to your request. I think it can be done by setting HttpWebRequest.Hostproperty.

如果您的网址是http://example.com/uri. 楼主自己解决。假设其1.2.3.4. 会的http://1.2.3.4/uri。现在将Host: example.com标头添加到您的请求中。我认为可以通过设置HttpWebRequest.Host属性来完成。

回答by mirko gatto

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/

我在 raspbian 上的单声道应用程序中遇到了同样的问题。我已经尝试了这个和其他线程中描述的不同解决方案,但都没有奏效。最终,我能够通过将 /etc/resolv.conf 中的名称服务器更改为谷歌服务器来解决问题https://developers.google.com/speed/public-dns/

Mirko

米尔科

回答by NG.

I know this is an old post, but was facing the same error, so thought to share the solution.

我知道这是一个旧帖子,但面临同样的错误,所以想分享解决方案。

  1. The best solution I found, when that exception occurs while the Wifi 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.
  2. This error can also raise if the user's Wifi is very unstable or the signal is very low. The same error occurs if there is no internet connection at all, even if connected to Wifi.
  1. 我发现的最好的解决方案是,当在 Wifi 连接时发生异常时,只是重试我的服务器调用,中间有轻微的睡眠。它大部分时间都有效,否则如果第二次调用失败,我将取消请求。
  2. 如果用户的 Wifi 非常不稳定或信号非常低,也会引发此错误。如果根本没有互联网连接,即使连接到 Wifi,也会发生同样的错误。

This is in line with my ans on :

这符合我的回答:

System.Net.WebException: Error: NameResolutionFailure when Calling WCF Services throwing exception in mono android application

System.Net.WebException: Error: NameResolutionFailure 在调用 WCF 服务时在单声道 android 应用程序中抛出异常

回答by Kim Rasmussen

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!

好吧,我使用 HttpClient - 但它可能是一个类似的问题。我在 Android 设备上遇到了同样的问题(它在 Windows Phone 上工作)......但是在我将 Host 添加到标题后它工作了!

client.DefaultRequestHeaders.Host = "mydomain.com";

client.DefaultRequestHeaders.Host = "mydomain.com";

You can still use the name in the url (you don't have to use the IP address)

您仍然可以在 url 中使用名称(您不必使用 IP 地址)

回答by gtrujillos

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.

当我在没有互联网连接的情况下启动移动应用程序(Android 或 iOS 无关紧要)时出现此错误。恢复连接后,每个请求都返回“NameResolutionFailure 异常”。我不得不等待 120 秒才能让 http 请求再次工作。在应用程序启动的任何位置设置以下代码行,错误终于消失了。

System.Net.ServicePointManager.DnsRefreshTimeout = 0;

The default DnsRefreshTimeout value is 120 seconds.

默认的 DnsRefreshTimeout 值为 120 秒。