无法在c#HttpWebRequest中使用外部代理

时间:2012-06-24 19:04:29

标签: c# httpwebrequest http-proxy

我正在点击这个网址:

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

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

链接不会打开任何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会打开,所以必须有办法。但我无法弄明白。

谢谢

1 个答案:

答案 0 :(得分:0)

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

祝你好运,让我知道它是如何为你效力的。