代码之家  ›  专栏  ›  技术社区  ›  Nate

System.Net.WebRequest请求不尊重主机文件

  •  9
  • Nate  · 技术社区  · 14 年前

    有没有办法得到一个 System.Net.WebRequest System.Net.WebClient 尊重 hosts lmhosts 文件?

    例如:在我的主机文件中,我有:

    10.0.0.1  www.bing.com
    

    当我尝试在浏览器(IE和FF)中加载Bing时,它无法按预期加载。

    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 尊重主机文件,但 忽略它?我需要更改什么才能使WebRequest尊重hosts文件?

    • 我在3个Win7和2个Vista框上复制了这个。唯一不变的是我公司的网络。
    • 我使用的是.NET3.5SP1和VS2008

    编辑

    根据@Richard Beier的建议,我试过了 System.Net 追踪。带跟踪 WebRequest 它本该失败的。但是我一转身 该行为恢复为意外的成功。我在同一台机器上以调试和发布模式复制了它。

    编辑2

    4 回复  |  直到 14 年前
        1
  •  7
  •   weshouman    4 年前

    我认为@Hans Passant已经发现了这个问题。看起来你在IE中有代理设置。

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

    这是因为您要求操作系统获取的IP地址 www.bing.com

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

    www.bing.com

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

    这是有效的/失败的,因为您已要求框架从特定服务器(通过IP)获取网页。即使您设置了代理,此代理仍无法连接到此IP地址。

    我希望这有帮助。

    乔纳森

        2
  •  3
  •   Richard Beier    14 年前

    Console.WriteLine(Dns.GetHostAddresses("www.bing.com")[0]);             // 10.0.0.1     
    var response = WebRequest.Create("http://www.bing.com/").GetResponse(); // * * *
    Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
    

    我在标有“***”的那行有个例外。以下是例外细节:

    System.Net.WebException was unhandled
      Message=Unable to connect to the remote server
      Source=System
      StackTrace:
           at System.Net.HttpWebRequest.GetResponse()
           at ConsoleApplication2.Program.Main(String[] args) in c:\Data\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 17
      InnerException: System.Net.Sockets.SocketException
           Message=A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.0.0.1:80
           Source=System
           ErrorCode=10060
    

    可能是早期.NET版本的问题,现在在.NET4/VS2010中修复了这个问题?您使用的是哪个版本的.NET?

    我还发现了这个 thread 从2007年开始,其他人也遇到了同样的问题。这里有一些很好的建议,包括:

    在上面的线程中,mariyaatanasova\u msft还说:“HttpWebRequest使用Dns.GetHostEntry以解析主机,因此您可能会从Dns.GetHostAddresses".

        3
  •  0
  •   Xxx Xxx    9 年前

    您应该覆盖默认代理。 HttpWebRequest&WebRequest将设置默认代理(如果存在于Internet Explorer和您的文件中) 主机 绕过 .

    request.Proxy = new WebProxy();
    

    try
    {
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.bing.com");
       request.Proxy = new WebProxy();
       request.Method = "POST";            
       request.AllowAutoRedirect = false;
    
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();               
       if (response.StatusCode == HttpStatusCode.OK)
       {
          //some code here
       }
    }
    catch (exception e)
    {
       //Some other code here
    }