簡體   English   中英

以編程方式設置默認代理而不是使用app.config

[英]set default proxy programmatically instead of using app.config

作為代理的后面,我的.Net 4.0 C#應用程序僅在存在具有以下內容的app.config時才有效:

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

既然我不想擁有app.config並且因為不建議嵌入app.config,那么與app.config中的xml chunk具有相同效果的C#代碼是什么?我在哪里放置它?

您可以使用WebRequest.DefaultWebProxyGlobalProxySelection.Select

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

要么

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

以下代碼對我有用:

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

您可以使用System.Net WebProxy

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

MSDN上的更多細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM