C# 如何在 .net HttpWebRequest 中自动检测/使用 IE 代理设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1155363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:32:29  来源:igfitidea点击:

How to AutoDetect/Use IE proxy settings in .net HttpWebRequest

c#proxyhttpwebrequestdetect

提问by Kumar

Is it possible to detect/reuse those settings ?

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

How ?

如何 ?

The exception i'm getting is This is the exception while connecting to http://www.google.com

我得到的例外是这是连接到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"

采纳答案by Rob Levine

HttpWebRequest will actually use the IE proxy settings by default.

默认情况下,HttpWebRequest 将实际使用 IE 代理设置。

If you don'twant to use them, you have to specifically override the .Proxy proprty to either null (no proxy), or the proxy settings of you choice.

如果你希望使用它们,你必须明确覆盖.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();

回答by John Saunders

This happens by default, if WebRequest.Proxy is not set explicitly (by default it's set to WebRequest.DefaultWebProxy).

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

回答by Vedran

For people having problems with getting this to play nice with ISA server, you might try to set up proxy in the following manner:

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

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

回答by Carl Onager

I was getting a very similar situation where the HttpWebRequest wasn't picking up the correct proxy details by default and setting the UseDefaultCredentials didn't work either. Forcing the settings in code however worked a treat:

我遇到了非常相似的情况,默认情况下 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;
}

and because this uses the default credentials it should not ask the user for their details.

并且由于它使用默认凭据,因此不应询问用户的详细信息。

Note that this is a duplicate of my answer posted here for a very similar problem: Proxy Basic Authentication in C#: HTTP 407 error

请注意,这是我在此处针对一个非常相似的问题发布的答案的副本:C# 中的代理基本身份验证:HTTP 407 错误