0

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;
}
Nick_F
  • 1,103
  • 2
  • 13
  • 30

1 Answers1

0

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

Nick_F
  • 1,103
  • 2
  • 13
  • 30