.NET中的HttpWebRequest NameResolutionFailure异常(在Ubuntu上具有Mono) [英] HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)

查看:865
本文介绍了.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

例如,服务器仍然可以解析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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆