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

发布于 2024-12-28 23:49:33 字数 1461 浏览 1 评论 0原文

我有一个通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

东风软 2025-01-04 23:49:33

您可以通过将主机名转换为 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 to Host property.

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.com header to your request. I think it can be done by setting HttpWebRequest.Host property.

作死小能手 2025-01-04 23:49:33

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

  1. 我找到的最好的解决方案是当 Wifi 发生异常时
    已连接,只是为了重试我的服务器调用,并稍微休眠一下
    之间。它在大多数情况下都有效,否则如果第二次调用
    失败我取消请求。
  2. 如果用户的 Wifi 非常不稳定或
    信号非常低。如果没有互联网也会出现同样的错误
    即使连接到 Wifi,也无法连接。

这与我的回答一致:

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

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.

This is in line with my ans on :

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

贱贱哒 2025-01-04 23:49:33

好吧,我使用 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)

感性不性感 2025-01-04 23:49:33

我在 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

一个人练习一个人 2025-01-04 23:49:33

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

System.Net.ServicePointManager.DnsRefreshTimeout = 0;

默认 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.

System.Net.ServicePointManager.DnsRefreshTimeout = 0;

The default DnsRefreshTimeout value is 120 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文