.NET HTTPWEBREQUESTでIEプロキシ設定をオートドテクト/使用する方法
-
18-09-2019 - |
質問
これらの設定を検出/再利用することは可能ですか?
どのように ?
私が取得している例外は、これが接続中の例外です 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"
解決
httpwebrequestは、実際にはデフォルトでIEプロキシ設定を使用します。
もし、あんたが しないでください それらを使用したい場合は、.Proxy Pusprtyを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エラー
ISAサーバーでこれをうまく再生することに問題がある人のために、次の方法でプロキシをセットアップしようとするかもしれません。
IWebProxy webProxy = WebRequest.DefaultWebProxy;
webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
myRequest.Proxy = webProxy;
これはデフォルトで発生します。WebRequest.Proxyが明示的に設定されていない場合(デフォルトではに設定されています WebRequest.DefaultWebProxy
).
所属していません StackOverflow