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

后端 未结 4 1754
情歌与酒
情歌与酒 2020-12-16 13:20

Is it possible to detect/reuse those settings ?

How ?

The exception i\'m getting is This is the exception while connecting to http://www.google.com



        
相关标签:
4条回答
  • 2020-12-16 13:33

    For people having problems with getting this to play nice with ISA server, you might try to set up proxy in the following manner:

    IWebProxy webProxy = WebRequest.DefaultWebProxy;
    webProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
    myRequest.Proxy = webProxy;
    
    0 讨论(0)
  • 2020-12-16 13:35

    HttpWebRequest will actually use the IE proxy settings by default.

    If you don't want to use them, you have to specifically override the .Proxy proprty to either null (no proxy), or the proxy settings of you choice.

     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();
    
    0 讨论(0)
  • 2020-12-16 13:49

    This happens by default, if WebRequest.Proxy is not set explicitly (by default it's set to WebRequest.DefaultWebProxy).

    0 讨论(0)
  • 2020-12-16 13:56

    I was getting a very similar situation where the HttpWebRequest wasn't picking up the correct proxy details by default and setting the UseDefaultCredentials didn't work either. Forcing the settings in code however worked a treat:

    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;
    }
    

    and because this uses the default credentials it should not ask the user for their details.

    Note that this is a duplicate of my answer posted here for a very similar problem: Proxy Basic Authentication in C#: HTTP 407 error

    0 讨论(0)
提交回复
热议问题