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

42,409

Solution 1

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();

Solution 2

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

Solution 3

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;

Solution 4

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

Share:
42,409

Related videos on Youtube

Kumar
Author by

Kumar

Updated on July 09, 2022

Comments

  • Kumar
    Kumar almost 2 years

    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

    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"
    
  • Kumar
    Kumar almost 15 years
    doesn't look like it as it's throwing an exception could there be a caveat perhaps
  • Kumar
    Kumar almost 15 years
    doesn't look like it I'm actually using the recaptcha control which uses the HTTPWebRequest and throws an exception
  • Rob Levine
    Rob Levine almost 15 years
    no - setting the WebRequest.Proxy to null bypasses all proxies. Leaving it "as is" lets it pick up the default proxy (it is not null by default).
  • Rob Levine
    Rob Levine almost 15 years
    it does behave as I say. You can demonstrate this using the above snippet of code and using Fiddler. See this answer from a related topic to see how to demostrate that this is the default behaviour: stackoverflow.com/questions/1112320/…
  • John Saunders
    John Saunders almost 15 years
    @Kumar: post the full exception. @Rob: You're right. By default it's set to WebRequest.DefaultWebProxy.
  • EricLaw
    EricLaw almost 15 years
    One caveat is that the default proxy settings are read from the registry on the application's startup. If you want them to be reloaded because they've changed, you must explicitly indicate that by setting the .Proxy property.
  • John Saunders
    John Saunders almost 15 years
    Eric, what if the IE settings include a script? See stackoverflow.com/questions/1160683/… if you get a chance.