set default proxy programmatically instead of using app.config

穿精又带淫゛_ 提交于 2019-12-09 05:16:47

问题


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:


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);



回答2:


The following code worked for me:

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



回答3:


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



来源:https://stackoverflow.com/questions/12050415/set-default-proxy-programmatically-instead-of-using-app-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!