Setting A Web Proxy Through Configuration In .NET Applications

Specially in enterprise environments, proxy servers are used to access the Internet.

In a Windows / Internet Explorer environments there is a proxy server configuration in Internet Properties > Connections > LAN settings > Proxy server.

Although these configurations are tightly connected to Internet Explorer, any well behaved Windows application should, at least, allow the user to choose to use them.

In applications targeting the .NET framework, these the proxy server can be set on a per call basis. Several networking classes have a Proxy property that receives a value implementing the IWebProxy interface.

In order for the Windows/ Internet Explorer configuration to be used, the application must be configured to use the default proxy settings.

This configuration is done in the machine or application file in the proxy element of the defaultProxy configuration section in the system.net section group:

<configuration>
  <system.net>
    <defaultProxy enabled="true">
      <proxy usesystemdefault="True"/>
    </defaultProxy>
  </system.net>
</configuration>


You can use this configuration also to set a specific proxy to be used by your application. In the following example, a proxy setting for using the Fiddler Tool is used:



<configuration>
  <system.net>
    <defaultProxy enabled="true">
      <proxy proxyaddress="http://ipv4.fiddler"/>
      <proxy proxyaddress="http://127.0.0.1:8888"/>
    </defaultProxy>
  </system.net>
</configuration>


Unfortunately, this is a MachineToApplication setting and, for that reason, is not allowed in the user settings configuration file, when in a shared installation. In these type of installations, the default Windows / Internet Explorer settings should be used as a default. If a user needs or wants to specify proxy server settings, application specific proxy server settings must be used but, assigning the user defined proxy server configuration to the GlobalProxySelection.Select property will allow its use for the entire application.



Updated: Corrected proxy address when using Fiddler following Eric Lawrence’s comment:



> <proxy proxyaddress="http://ipv4.fiddler/" />

That line should not work. Fiddler doesn’t register anything in DNS, so for "ipv4.fiddler" to have any meaning, Fiddler must already be being used as the proxy.

The proper setting for Fiddler use should be:

<proxy proxyaddress="http://127.0.0.1:8888" />

4 thoughts on “Setting A Web Proxy Through Configuration In .NET Applications”

  1. Cannot make this work … I am performing this change in myprogram.exe.config file.
    Any hints??

    Thanks!

  2. Hello, this is not working for me either. I’m putting the config into the dll.config and the exe.config.

    I have a program that opens a WebBrowser control and the data the browser sends do not go thru Fiddler :S

    Can you help me? Thanks!!!!

  3. The WebBrowser control uses the same proxy settings as Internet Explorer. To change them, you’ll have to go through Internet Options.

    If you ran the page directly in Internet Explorer don’t you catch it in Fiddler?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>