如何在.net HttpWebRequest中自动检测/使用IE代理设置
是否可以检测/重用这些设置?
怎么样 ?
我得到的例外是这是连接到https://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"
HttpWebRequest实际上默认使用IE代理设置。
如果您不想使用它们,则必须专门将.Proxy属性覆盖为null(无代理)或您选择的代理设置。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://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#中的代理基本身份validation:HTTP 407错误
对于在使用ISA服务器时遇到问题的人,您可能会尝试以下列方式设置代理:
IWebProxy webProxy = WebRequest.DefaultWebProxy; webProxy.Credentials = CredentialCache.DefaultNetworkCredentials; myRequest.Proxy = webProxy;
如果未明确设置WebRequest.Proxy,则默认情况下会发生这种情况(默认情况下,它设置为WebRequest.DefaultWebProxy
)。
上述就是C#学习教程:如何在.net HttpWebRequest中自动检测/使用IE代理设置分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1001581.html