如何在.net中的HttpWebRequest自动检测/使用IE代理服务器设置(How to Aut

2019-06-26 09:52发布

是否有可能检测/重复使用这些设置吗?

怎么样 ?

我发现了例外的是这是个例外,而连接到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"

Answer 1:

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

如果你不想使用它们,你必须明确覆盖.Proxy proprty为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();


Answer 2:

我得到其中的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;
}

因为这种使用默认凭据不应该要求他们的详细信息用户。

请注意,这是我的答案贴在这里的一个非常类似的问题的重复: 代理服务器基本身份验证在C#中:HTTP 407错误



Answer 3:

另外,具有得到这个播放使用ISA服务器漂亮的问题的人,你可以尝试设置代理的方式如下:

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


Answer 4:

这发生在默认情况下,如果没有明确设置WebRequest.Proxy(默认情况下它被设置为WebRequest.DefaultWebProxy )。



文章来源: How to AutoDetect/Use IE proxy settings in .net HttpWebRequest