Get IE's default proxy with DefaultWebProxy

Paul Matthews picture Paul Matthews · Jun 13, 2013 · Viewed 11.4k times · Source

I've read through pretty much all the documentation I can find but I'm yet to find a simple working example of how to get IE's default proxy settings using DefaultWebProxy().

This code seems to compile and work but how do I then go ahead and get the proxy URI as a string?

HttpWebRequest webRequest = 
    (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");

if (WebRequest.DefaultWebProxy != null)
{
    webRequest.Proxy = WebRequest.DefaultWebProxy;
}

EDIT:

Since submitting this question I have found that one or many proxies can be set for different destinations or bypassed (perhaps for local intranet destinations). That's why you need to specify a URI to GetProxy(). It needs to know which destination to get the proxy for. If "Automatically Detect Settings" is set in Internet Options the browser will look for a PAC file on your local domain. The PAC file contains a Javascript function which determines the proxy address for a given destination.

Answer

zmbq picture zmbq · Jun 13, 2013

WebRequest.DefaultWebProxy implements the IWebProxy interface. You can use the GetProxy method to get the proxy's URI:

var uri = WebRequest.DefaultWebProxy.GetProxy(new Uri("http://www.google.com"));

Response to a comment:

You need to pass the uri to GetProxy because that's how Microsoft implemented it...

Seriously, I believe this is so because you can configure the browser to bypass the proxy for some addresses. If you pass one of this bypassed addresses, you will probably get a different result.