15

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?

1
  • I need a solution to this problem so bad!! I've been consistently hitting this problem in a number of environments for years now... Commented Jun 2, 2016 at 0:24

3 Answers 3

18

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);
Sign up to request clarification or add additional context in comments.

1 Comment

System.Net.GlobalProxySelection is deprecated.
9

The following code worked for me:

System.Net.WebRequest.DefaultWebProxy.Credentials 
    = System.Net.CredentialCache.DefaultNetworkCredentials;

Comments

2

you can use WebProxy from System.Net

WebProxy proxyObject = new WebProxy("PROXYIP",PORTNO);
WebRequest req = WebRequest.Create("http://www.stackoverflow.com");
req.Proxy = proxyObject;

More details at MSDN

1 Comment

This would not change the proxy for the whole app like the app.config would, just this single request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.