17

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"
m__
  • 1,721
  • 1
  • 17
  • 30
Kumar
  • 10,997
  • 13
  • 84
  • 134
  • If you're getting an exception, please post the whole thing: ex.ToString(). – John Saunders Jul 20 '09 at 19:36
  • This is more likely a firewall problem. You're just not getting a connection. – John Saunders Jul 21 '09 at 19:01
  • Ok, i copied the code into a new console app and it worked right off the bat it looks like some permissioning in the vs2008 built in webserver any clues on what/where to fix that ultimately the code needs to run recaptcha so it has to execute in the webhost – Kumar Jul 21 '09 at 19:07
  • aaargh, i always forget the formatting in the comments does not handle CR !!! ....................Anyways, it looks like there's some permissioning/restriction in the iis sandbox.........................any pointers ? – Kumar Jul 21 '09 at 19:09

4 Answers4

25

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
  • 40,328
  • 13
  • 85
  • 111
  • doesn't look like it I'm actually using the recaptcha control which uses the HTTPWebRequest and throws an exception – Kumar Jul 20 '09 at 19:22
  • 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 Jul 20 '09 at 19:25
  • 3
    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 Jul 21 '09 at 14:42
  • 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 Jul 21 '09 at 19:03
9

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

Alexander
  • 9,104
  • 1
  • 17
  • 41
Carl Onager
  • 4,112
  • 2
  • 38
  • 66
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;
Vedran
  • 10,369
  • 5
  • 50
  • 57
1

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

Marek
  • 10,307
  • 8
  • 70
  • 106
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • doesn't look like it as it's throwing an exception could there be a caveat perhaps – Kumar Jul 20 '09 at 19:21
  • 3
    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 Jul 20 '09 at 19:24
  • @Kumar: post the full exception. @Rob: You're right. By default it's set to `WebRequest.DefaultWebProxy`. – John Saunders Jul 20 '09 at 19:35