Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  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"
like image 623
Kumar Avatar asked Jul 20 '09 19:07

Kumar


4 Answers

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();
like image 123
Rob Levine Avatar answered Nov 06 '22 15:11

Rob Levine


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

like image 37
Carl Onager Avatar answered Nov 06 '22 17:11

Carl Onager


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;
like image 3
Vedran Avatar answered Nov 06 '22 16:11

Vedran


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

like image 1
John Saunders Avatar answered Nov 06 '22 15:11

John Saunders