是否可以检测/重用这些设置?
怎么样 ?
我得到的例外情况是这是连接到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"
Rob Levine.. 25
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();
需要注意的是,在应用程序启动时从注册表中读取默认代理设置.如果由于它们已更改而希望重新加载它们,则必须通过设置.Proxy属性来明确指出. (2认同)
Carl Onager.. 7
我得到了一个非常类似的情况,默认情况下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错误
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();
我得到了一个非常类似的情况,默认情况下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错误