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

HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)(.NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono))

本文介绍了.NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

该程序每分钟左右通过 HttpWebRequest 下载一个网页,这在大多数情况下都可以正常工作:

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

服务器还是abel解析DNS,例如

The server is still abel to resolve DNS, for example

 wget http://www.google.com

将在没有任何问题的情况下返回文件,ping 和其他解析 DNS 的命令也将如此.

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.

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

I have checked open file counts on the system (400 ish), memory usage (327mb of 4gb), CPU usage (2-3%) and all are OK.

有什么想法吗?

推荐答案

可以通过将主机名翻译成ip并将主机名添加到Headers集合或Host 属性.

You can solve it by translating the host name to ip and add the host name to Headers collection or to Host property.

如果您的网址是 http://example.com/uri.自己解决主机.假设它的 1.2.3.4.它将是 http://1.2.3.4/uri.现在将 Host: example.com 标头添加到您的请求中.我认为可以通过设置 HttpWebRequest.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.com header to your request. I think it can be done by setting HttpWebRequest.Host property.

这篇关于.NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:.NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono)

基础教程推荐