簡體   English   中英

如何在 .net HttpWebRequest 中自動檢測/使用 IE 代理設置

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

是否可以檢測/重用這些設置?

如何 ?

我得到的例外是這是連接到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 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 錯誤

對於在使用 ISA 服務器時遇到問題的人,您可以嘗試按以下方式設置代理:

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

如果未明確設置WebRequest.DefaultWebProxy (默認設置為WebRequest.DefaultWebProxy ),則默認情況下會發生這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM