17

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

如何 ?

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

4 回答 4

25

默认情况下,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();
于 2009-07-20T19:21:05.160 回答
8

我遇到了一个非常相似的情况,默认情况下 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 错误

于 2013-08-02T09:34:37.200 回答
3

对于无法让这个与 ISA 服务器配合使用的人,您可以尝试以下列方式设置代理:

IWebProxy webProxy = WebRequest.DefaultWebProxy;
webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
myRequest.Proxy = webProxy;
于 2013-06-28T14:23:36.487 回答
1

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

于 2009-07-20T19:19:06.677 回答