【问题标题】:Unable to use external proxy in c# HttpWebRequest无法在 c# HttpWebRequest 中使用外部代理
【发布时间】:2012-06-24 19:04:28
【问题描述】:

我正在点击这个网址:

http://www.google.co.uk/search?q=online stores uk&hl=en&cr=countryUK%7CcountryGB&as_qdr=all&tbs=ctr:countryUK

基本上我得到了 ppcUrls,它在没有任何代理的情况下完美运行。

但是当我尝试使用互联网上可用的代理时:

http://proxy-list.org/en/index.php?pp=3128&pt=any&pc=any&ps=any&submit=Filter+Proxy

上面的链接不会以任何方式打开:|,我确实用 Internet Explorer 检查了 ipz,它打开了,但是在 HTTPWEBREQUEST 中,有时我得到 503 服务器不可用,或者太 许多重定向

链接不会以任何 ip 打开。

有什么建议吗?下面是我获取 HTML 函数:

   public string getHtml(string url, string proxytmp)
    {
        string responseData = "";
        try
        {


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*";
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            request.Timeout = 60000;
            request.Method = "GET";

            if (proxies.Count > 0)
            {
                try
                {
                    int customIP = 0;
                    int port = 0;
                    string ip = string.Empty;

                    string[] splitter = proxytmp.Split(':');
                    if (splitter.Length > 0)
                    {

                        ip = splitter[0].ToString();
                        port = Convert.ToInt32(splitter[1].ToString());
                    }

                    WebProxy proxy = new WebProxy(ip, port);
                    request.Proxy = proxy;

                }
                catch (Exception exp)
                {

                }
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(responseStream);
                responseData = myStreamReader.ReadToEnd();
            }
            response.Close();


        }
          catch (System.Exception e)
        {
            responseData = e.ToString();
        }

        return responseData;

    }

更新

当我使用与 Internet Explorer 相同的代理时,Url 会打开,所以一定有办法。但我想不通。

谢谢

【问题讨论】:

    标签: c# httpwebrequest http-proxy


    【解决方案1】:

    我的猜测是代理会阻止某种性质的传入连接,这就是您遇到各种问题的原因,这些检查在性质上可能很复杂,或者可能就像将 User-Agent 设置为有效的浏览器一样简单.. 我不确定代理可以检查哪些其他内容,我建议您在使用浏览器发出请求并在 C# 中进行相应更改时查看创建的请求对象(例如 Referrer、Port 等)代码..

    祝你好运,让我知道你的效果如何。

    【讨论】:

    • 但它确实适用于google.co.uk 并获取 HTML 但不适用于上述内容..
    • 运气不好,我几乎用 fiddler 复制了准确​​的内容,但仍然没有运气。
    • 运气好吗?我也有同样的问题。
    • @confusedMind 您是使用注册表设置代理还是有其他方法?
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2014-12-26
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多