2010-10-12 90 views
1

嗨 下面的代碼工作正常,指示系統不要使用代理服務器而不自動檢測代理服務器,這會導致無代碼延遲。然而,在使用代理的網絡上,我只是得到底層連接已關閉! 所以四個問題:Poxy代理問題! c#

  1. 我是否正確指定代理?
  2. 如果是這樣,我該如何告訴它使用默認代理憑據?
  3. 應該使用想要指定憑據如何設置?
  4. 如何將其恢復到原始狀態?


if (!Properties.Settings.Default.UseProxyServer){ 
    //set the system not to use a proxy server 
    //saves the delay seen when browser set to auto detect proxy and not proxy 
    //is used. This works well!! 
    WebRequest.DefaultWebProxy = new WebProxy(); 
} 
else{ 
    WebRequest.DefaultWebProxy = 
     new WebProxy(proxyServerAddress, proxyServerPort); 
    //proxyServerPort is an int. 
    //How do I add default credentials?? 
} 

WebClient client = new WebClient(); 
//specify an encoding for uploading. 
client.Encoding = System.Text.Encoding.ASCII; 
// Upload the data. 
var myReply = client.UploadValues(addressURL, data); 

我需要這個代碼不是在app.config中。

感謝

回答

1

您可以創建一個Web代理對象

var proxy = new WebProxy("http://server:8080"); 
proxy.credentials = new system.net.Credentials.DefaultCredenialCache; 
proxy.Other properties 

您還可以創建一個配置

<configuration> 
    <system.net> 
    <defaultProxy> 
     <proxy 
     usesystemdefaults="true" 
     proxyaddress="http://192.168.1.10:3128" 
     bypassonlocal="true" 
     /> 
     <bypasslist 
     <add address="[a-z]+\.contoso\.com" /> 
     </bypasslist> 
    </defaultProxy> 
    </system.net> 
</configuration> 
+0

感謝,但我需要更改默認代理。 我想我有它!但我需要等到使用代理服務器回到網絡時,我現在正在執行以下操作。 WebProxy wp = new WebProxy(proxyServerAddress); wp.UseDefaultCredentials = true; WebRequest.DefaultWebProxy = wp; – Adrian 2010-10-12 20:35:22