以编程方式设置默认代理而不是使用 app.config [英] set default proxy programmatically instead of using app.config

查看:38
本文介绍了以编程方式设置默认代理而不是使用 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代理之后,我的 .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,那么与 app.config 中的 xml 块具有相同效果的 C# 代码是什么,我应该将其放置在哪里它吗?

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.DefaultWebProxyGlobalProxySelection.Select

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

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

这篇关于以编程方式设置默认代理而不是使用 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆