set default proxy programmatically instead of using app.config

Posted on

Problem :

Being behind a proxy, my .Net 4.0 C# application only works when there is an app.config with the following content:

    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy />
        <bypasslist />
        <module />

Now since I don’t want to have an app.config and since embedding app.config is not recommended, what is the C# code that has the same effect as that xml chunk in the app.config and where do I place it?

Solution :

You can use WebRequest.DefaultWebProxy or GlobalProxySelection.Select

System.Net.GlobalProxySelection.Select = new WebProxy(ip,port);


System.Net.WebRequest.DefaultWebProxy = new WebProxy(ip,port);

The following code worked for me:

    = System.Net.CredentialCache.DefaultNetworkCredentials;

you can use WebProxy from System.Net

WebProxy proxyObject = new WebProxy("PROXYIP",PORTNO);
WebRequest req = WebRequest.Create("");
req.Proxy = proxyObject;

More details at MSDN

Leave a Reply

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