Change webclient proxy with every new request

181 Views Asked by At

How can I change the proxy every time when I make a new request? If I start the application and run this request multiple times, I can see it uses the first proxy used, even if I change the parameters for proxy from a list of proxy servers every time I call this function. I tried to dispose of the client and also to set the client proxy to null, but it didn't help. If I close the program and restart it, the proxy uses a different IP address.

string GetRequest() 
{
  WebClient client = new WebClient();
  WebProxy wp = new WebProxy(randomProxyAddress + ":" + portNum);
  wp.UseDefaultCredentials = false;
  wp.Credentials = new NetworkCredential(username, password);
  client.Proxy = wp;            
  string str = client.DownloadString("http://www.example.com");
  client.Proxy = null;
  client.Dispose();
  return str;
}
1

There are 1 best solutions below

0
Nick_F On

I found out I had to set request.KeepAlive = false in WebClient.