2010-10-12 41 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