System.Net.WebRequest不尊重hosts文件 [英] System.Net.WebRequest not respecting hosts file

查看:94
本文介绍了System.Net.WebRequest不尊重hosts文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一个 System.Net.WebRequest System.Net.WebClient 尊重主机 LMHOSTS 文件?

Is there a way to get a System.Net.WebRequest or System.Net.WebClient to respect the hosts or lmhosts file?

例如:在我的hosts文件,我有:

For example: in my hosts file I have:

10.0.0.1  www.bing.com

当我尝试加载冰在浏览器(IE和FF)它不能正常加载。

When I try to load Bing in a browser (both IE and FF) it fails to load as expected.

Dns.GetHostAddresses("www.bing.com")[0]; // 10.0.0.1 
WebRequest.Create("http://10.0.0.1").GetResponse(); // throws exception (expected)
WebRequest.Create("http://www.bing.com/").GetResponse(); // unexpectedly succeeds

同样的:

WebClient wc = new WebClient();
wc.DownloadString("http://www.bing.com"); //succeeds 

为什么 System.Net.Dns 尊重hosts文件,但 System.Net.WebRequest 忽略它?需要什么改变,使主机文件的WebRequest的尊重?

Why would System.Net.Dns respect the hosts file but System.Net.WebRequest ignore it? What do I need to change to make the WebRequest respect the hosts file?

附加信息:

  • 如果我禁用IPv6,并把我的IPv4的DNS服务器为127.0.0.1,上面的code ++工程(失败),符合市场预期。但是,如果将我的正常的DNS服务器重新候补委员,意外的行为将继续。
  • 在我3 Win7和Vista的2盒转载此。唯一不变的是我公司的网络。
  • 在我使用.NET 3.5 SP1和VS2008

修改

每@Richard贝尔的建议下,我尝试了 System.Net 跟踪。随着追查的打开的WebRequest 失败,因为它应该。但是当我打开跟踪关闭行为恢复到意想不到的成功。我已经在调试和发布模式为前转载此在同一机器上。

Per @Richard Beier's suggestion, I tried out System.Net tracing. With tracing ON the WebRequest fails as it should. However as soon as I turn tracing OFF the behavior reverts to the unexpected success. I have reproduced this on the same machines as before in both debug and release mode.

编辑2

这竟然是该公司代理给我们的问题。我们的解决方案是一个自定义代理配置脚本我们的测试机器上有bing.com点直接默认代理代替。

This turned out to be the company proxy giving us issues. Our solution was a custom proxy config script for our test machines that had "bing.com" point to DIRECT instead of the default proxy.

推荐答案

我觉得@Hans帕桑特曾在这里发现的问题。它看起来就像你在IE浏览器的代理设置。

I think that @Hans Passant has spotted the issue here. It looks like you have a proxy setup in IE.

Dns.GetHostAddresses("www.bing.com")[0]; // 10.0.0.1  

这工作,因为你是问操作系统获取的IP地址www.bing.com

This works because you are asking the OS to get the IP addresses for www.bing.com

WebRequest.Create("http://www.bing.com/").GetResponse(); // unexpectedly succeeds

这工作,因为,因为你所要求的框架来从服务器名称的路径。该框架采用了相同的发动机和设置,IE浏览器的前端使用,因此,如果你的公司已指定您使用公司代理服务器GPO,它是将IP地址解析为www.bing.com,而不是你的代理服务器。

This works because because you are asking the framework to fetch a path from a server name. The framework uses the same engine and settings that IE frontend uses and hence if your company has specified by a GPO that you use a company proxy server, it is that proxy server that resolves the IP address for www.bing.com rather than you.

WebRequest.Create("http://10.0.0.1").GetResponse(); // throws exception (expected) 

这工作/失败,因为你已经问了框架,把你从一个特定的服务器(通过IP)的网页。即使你有一个代理设置,该代理仍然不会比布尔连接到该IP地址。

This works/fails because you have asked the for the framework to fetch you a webpage from a specific server (by IP). Even if you do have a proxy set, this proxy will still not beable to connect to this IP address.

我希望这会有所帮助。

乔纳森

这篇关于System.Net.WebRequest不尊重hosts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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