【问题标题】:How to AutoDetect/Use IE proxy settings in .net HttpWebRequest如何在.net HttpWebRequest 中自动检测/使用 IE 代理设置
【发布时间】:2010-11-12 10:12:39
【问题描述】:

是否可以检测/重用这些设置?

如何?

我得到的例外是 这是连接到http://www.google.com时的异常

System.Net.WebException: Unable to connect to the remote server --->
  System.Net.Sockets.SocketException: 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 66.102.1.99:80

  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
     SocketAddress socketAddress)
  at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
     Socket s4, Socket s6, Socket& socket, IPAddress& address,
     ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
     Exception& exception)
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.GetResponse()
  at mvcTest.MvcApplication.Application_Start() in
     C:\\home\\test\\Application1\\Application1\\Program.cs:line 33"

【问题讨论】:

  • 如果您遇到异常,请发布整个内容:ex.ToString()。
  • 这很可能是防火墙问题。你只是没有连接。
  • 好的,我将代码复制到一个新的控制台应用程序中,它可以立即运行它看起来像是在内置的网络服务器 vs2008 中获得了一些关于最终代码需要修复什么/在哪里的线索运行 recaptcha 所以它必须在 webhost 中执行
  • aaargh,我总是忘记 cmets 中的格式无法处理 CR !!! ...................无论如何,看起来 iis 沙箱中有一些许可/限制...... ......任何指针?

标签: c# proxy httpwebrequest detect


【解决方案1】:

如果未明确设置 WebRequest.Proxy(默认设置为 WebRequest.DefaultWebProxy),则默认情况下会发生这种情况。

【讨论】:

  • 看起来不像,因为它抛出异常可能有警告
  • 否 - 将 WebRequest.Proxy 设置为 null 会绕过所有代理。保持“原样”让它选择默认代理(默认情况下它不为空)。
  • @Kumar:发布完整的异常。 @Rob:你是对的。默认设置为WebRequest.DefaultWebProxy
【解决方案2】:

HttpWebRequest实际上会默认使用IE代理设置。

如果您不想使用它们,则必须将 .Proxy 属性专门覆盖为 null(无代理)或您选择的代理设置。

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://news.bbc.co.uk");
 //request.Proxy = null; // uncomment this to bypass the default (IE) proxy settings
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 Console.WriteLine("Done - press return");
 Console.ReadLine();

【讨论】:

  • 看起来不像我实际上是在使用 recaptcha 控件,它使用 HTTPWebRequest 并引发异常
  • 它的行为和我说的一样。您可以使用上面的 sn-p 代码并使用 Fiddler 来演示这一点。请参阅相关主题的此答案,以了解如何证明这是默认行为:stackoverflow.com/questions/1112320/…
  • 需要注意的是,默认代理设置是在应用程序启动时从注册表中读取的。如果您希望它们因已更改而重新加载,则必须通过设置 .Proxy 属性来明确指出这一点。
  • Eric,如果 IE 设置包含脚本怎么办?如果有机会,请参阅stackoverflow.com/questions/1160683/…
【解决方案3】:

如果有人在使用 ISA 服务器时遇到问题,您可以尝试按以下方式设置代理:

IWebProxy webProxy = WebRequest.DefaultWebProxy;
webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
myRequest.Proxy = webProxy;

【讨论】:

    【解决方案4】:

    我遇到了一个非常相似的情况,默认情况下 HttpWebRequest 没有获取正确的代理详细信息,并且设置 UseDefaultCredentials 也不起作用。然而,在代码中强制设置是一种享受:

    IWebProxy proxy = myWebRequest.Proxy;
    if (proxy != null) {
        string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString();
        myWebRequest.UseDefaultCredentials = true;
        myWebRequest.Proxy = new WebProxy(proxyuri, false);
        myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    }
    

    并且因为这使用默认凭据,所以它不应该询问用户他们的详细信息。

    请注意,这是我在此处针对一个非常相似的问题发布的答案的副本:Proxy Basic Authentication in C#: HTTP 407 error

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 2011-06-23
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2010-09-16
      相关资源
      最近更新 更多