WebProxy URI无效

时间:2016-08-20 08:15:36

标签: c# webproxy

这是我目前的代码

using (WebClient client = new WebClient()) {
    WebProxy proxy = new WebProxy();
    proxy.Address = new Uri(96.44.147.138:6060);
    proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
    proxy.UseDefaultCredentials = false;
    proxy.BypassProxyOnLocal = false;
    Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}

代理需要凭据。

我在第proxy.Address = new Uri(96.44.147.138:6060);行收到错误 说

  

“URI方案无效。”

不确定它期待什么样的价值

2 个答案:

答案 0 :(得分:1)

必须像;

using (var client = new WebClient())
{
    var proxy = new WebProxy();

    proxy.Address = new Uri("http://96.44.147.138:6060");
    proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
    proxy.UseDefaultCredentials = false;
    proxy.BypassProxyOnLocal = false;

    Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}

示例编辑:Setting a global HTTP proxy in C# and .NET client classes

答案 1 :(得分:0)

Uri应由scheme host和optiona port组成。所以你应该使用

proxy.Address = new Uri("http://96.44.147.138:6060");