【问题标题】:Posting data through http, switch between proxies after each request通过http发布数据,每次请求后在代理之间切换
【发布时间】:2013-04-14 21:32:44
【问题描述】:

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

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

我找到了这个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();

【问题讨论】:

    标签: c# .net proxy http-post


    【解决方案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);
       }
    }
    

    【讨论】:

    • 我应该包含什么命名空间才能使用 webproxy/
    • @JoeySalacHipolito WebProxy - 第一行写着 Namespace: System.Net...
    • @Alexie Levenkov,如果我返回$_SERVER['REMOTE_ADDR'];,为什么我会得到::1
    • @JoeySalacHipolito 它是您的 IPv6 地址(相当于 IPv4 中的 127.0.0.1)。
    • 是的,这意味着我是用我的电脑发帖的吧?我只是使用了代理吗?
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2015-11-21
    相关资源
    最近更新 更多