Wednesday, April 6, 2011

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

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\r\n at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)\r\n at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)\r\n at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)\r\n --- End of inner exception stack trace ---\r\n at System.Net.HttpWebRequest.GetResponse()\r\n at mvcTest.MvcApplication.Application_Start() in C:\home\test\Application1\Application1\Program.cs:line 33"

From stackoverflow
  • This happens by default, if WebRequest.Proxy is null.

    Kumar : doesn't look like it as it's throwing an exception could there be a caveat perhaps
    Rob Levine : 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).
    John Saunders : @Kumar: post the full exception. @Rob: You're right. By default it's set to `WebRequest.DefaultWebProxy`.
  • 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();
    
    Kumar : doesn't look like it I'm actually using the recaptcha control which uses the HTTPWebRequest and throws an exception
    Rob Levine : 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: http://stackoverflow.com/questions/1112320/removing-obsolete-webproxy-getdefaultproxy-references/1112399#1112399
    EricLaw -MSFT- : 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 : Eric, what if the IE settings include a script? See http://stackoverflow.com/questions/1160683/whats-the-right-way-to-handle-a-proxy-autoconfig-script-to-make-a-webservice-cal if you get a chance.

0 comments:

Post a Comment