.NET中的HttpWebRequest NameResolutionFailure异常(在Ubuntu上具有Mono) [英] HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)
问题描述
我有一个通过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屋!