通过http发布数据,在每次请求后在代理之间切换

时间:2013-04-22 04:16:42

标签: c# .net proxy http-post

我对C#相对较新,特别是在网络方面。

假设我有以下内容:

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);
}

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

我发现了Related Link

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();

1 个答案:

答案 0 :(得分: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);
   }
}