views:

2176

answers:

2

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"

A: 

This happens by default, if WebRequest.Proxy is null.

John Saunders
doesn't look like it as it's throwing an exceptioncould there be a caveat perhaps
Kumar
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
@Kumar: post the full exception. @Rob: You're right. By default it's set to `WebRequest.DefaultWebProxy`.
John Saunders
+2  A: 

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();
Rob Levine
doesn't look like itI'm actually using the recaptcha control which uses the HTTPWebRequest and throws an exception
Kumar
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
Rob Levine
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.
EricLaw -MSFT-
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.
John Saunders