您好 下面的代码可以很好地指示系统不使用代理并且不自动检测代理,这会导致没有代码的延迟。但是,在具有代理的网络上,我只是关闭了底层连接! 所以有四个问题:
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中的代码中使用它。
由于
答案 0 :(得分: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>
答案 1 :(得分:0)