Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set default proxy programmatically instead of using app.config

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

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

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?

like image 900
user1595494 Avatar asked Aug 21 '12 07:08

user1595494


2 Answers

You can use WebRequest.DefaultWebProxy or GlobalProxySelection.Select

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

OR

System.Net.WebRequest.DefaultWebProxy = new WebProxy(ip,port);
like image 163
L.B Avatar answered Oct 05 '22 03:10

L.B


The following code worked for me:

System.Net.WebRequest.DefaultWebProxy.Credentials 
    = System.Net.CredentialCache.DefaultNetworkCredentials;
like image 25
Patrick McDonald Avatar answered Oct 05 '22 03:10

Patrick McDonald