0

我对 C# 比较陌生,尤其是在它的 Web 方面。

假设我有以下内容:

string URI = "http://www.domain.com/post.php";
string params = "param1=value1&param2=value2&param3=value3";

我知道我可以发布这样的数据:

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, params);
}

但是在发布这些数据时如何使用代理?

我找到了这个相关链接

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
WebProxy myproxy = new WebProxy("1.1.1.1", 80);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
4

1 回答 1

1

WebClient.Proxy是您正在寻找的属性:

WebProxy[] myproxies = new WebProxy[] { new WebProxy("1.1.1.1", 80), 
     new WebProxy("1.1.1.2", 80) };
var currentProxy = 0;

while (true)
{
   // set proxy to new one every iteration...
   currentProxy = (currentProxy + 1) % myproxies.Length;
   using (WebClient wc = new WebClient())
   {
     wc.Proxy = myproxies[currentProxy]; 
     wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
     string HtmlResult = wc.UploadString(URI, params);
   }
}
于 2013-04-22T04:22:24.760 回答