Dgameman1
Dgameman1

Reputation: 378

C# Using a proxy for WebClient

This is the code I have right now

using (WebClient client = new WebClient()) 
{
    Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}

Now, I would like to put in 2 options for a proxy.

  1. An option to either a standard IP:Proxy
  2. An option with authentication.

I just can't seem to figure out how to connect it with the above code

Upvotes: 3

Views: 5837

Answers (1)

Dgameman1
Dgameman1

Reputation: 378

    using (WebClient client = new WebClient()) {
        client.Proxy = new WebProxy("31.4.5.26", 8080); // proxy's host,port
        client.Proxy.Credentials = new NetworkCredential("proxyuser", "proxypassword");
        Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
    }

Works fine for me!

Upvotes: 3

Related Questions