This is easily achieved either programmatically, in your code, or declaratively in either the web.config or the app.config.
You can programmatically create a proxy like so:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[ultimate destination of your request]");
WebProxy myproxy = new WebProxy("[your proxy address]", [your proxy port number]);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
You're basically assigning the WebProxy object to the request object's proxy property. This request will then use the proxy you define.
To achieve the same thing declaratively, you can do the following:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[your proxy address and port number]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
within your web.config or app.config. This sets a default proxy that all http requests will use. Depending upon exactly what you need to achieve, you may or may not require some of the additional attributes of the defaultProxy / proxy element, so please refer to the documentation for those.
Automatic proxy detection is a process by which a Web proxy server is identified by the
system and used to send requests on behalf of the client. This feature is also known as
Web Proxy Auto-Discovery (WPAD). When automatic proxy detection is enabled, the system
attempts to locate a proxy configuration script that is responsible for returning the set of proxies that can be used for the request.
Try this code. Call it before making any http requests. The code will use the proxy from your Internet Explorer Settings - one thing though, I use proxy.Credentials = .... because my proxy server is an NTLM authenticated Internet Acceleration Server. Give it a whizz.
Foole's code worked perfectly for me, but in .NET 4.0, don't forget to check if Proxy is NULL, which means no proxy configured (outside corporate environment)
So here's the code that solved my problem with our corporate proxy
WebClient web = new WebClient();
if (web.Proxy != null)
web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;