Poxy Proxy Problem! c#
Hi The code below works fine to instruct the system not to use a proxy and to not auto detect one, which causes a delay without the code. However while on a network with a proxy I just get the underlying connection is closed! So four questions:
- Am I specifying the proxy correctly?
- If so how do I tell it to use default proxy credentials?
- Should the used want to 开发者_JS百科specify credentials how are they set?
- How do I set it back to the original state?
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);
I need to this in code not in the app.config.
Thanks
You can create a Web proxy object
var proxy = new WebProxy("http://server:8080");
proxy.credentials = new system.net.Credentials.DefaultCredenialCache;
proxy.Other properties
You can also create a config
<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>
Try this:
http://weblogs.asp.net/jan/archive/2004/01/28/63771.aspx
You may also want to check this out:
http://geekswithblogs.net/ranganh/archive/2005/08/29/51474.aspx
精彩评论