设置默认的代理编程,而不是使用的app.config而不是、app、config

2023-09-03 02:02:15 作者:街灯下的背景

作为一个代理之后,我的.Net 4.0 C#应用程序只能在有一个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>

现在,因为我不希望有一个app.config而且由于嵌入的app.config不推荐,什么是C#code具有同等效力XML块在App.config在哪里我把它?

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?

推荐答案

您可以使用WebRequest.DefaultWebProxy或GlobalProxySelection.Select

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

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