tags:

views:

96

answers:

2

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:

  1. Am I specifying the proxy correctly?
  2. If so how do I tell it to use default proxy credentials?
  3. Should the used want to specify credentials how are they set?
  4. 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

+1  A: 

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>
Iain
Thanks for this but I need to change the default proxy.I think I have it! but I need to wait until back on a network with a proxy I'm now doing the below. WebProxy wp = new WebProxy(proxyServerAddress); wp.UseDefaultCredentials = true; WebRequest.DefaultWebProxy = wp;
Adrian