简体   繁体   English

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

[英]Unable to use external proxy in c# HttpWebRequest

i am hitting this url: 我打这个网址:

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

Basically i get the ppcUrls, it works perfect without any proxy. 基本上我得到了ppcUrls,它完美运行而无需任何代理。

But when i try to use a proxy which are available on the internet: 但是,当我尝试使用互联网上提供的代理时:

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

The above link wont open in any way :|, i did check the ipz with the internet explorer and it opened , but here in HTTPWEBREQUEST , sometime i get 503 Server unavailable , or Too Many redirections 上面的链接不会以任何方式打开:|,我确实用Internet Explorer检查了ipz并打开了它,但是在HTTPWEBREQUEST中,有时我无法使用503服务器 ,或重定向太多

The link wont open with any ip . 该链接不会以任何ip打开。

Any suggestion ? 有什么建议吗? Below is my getting HTML function: 下面是我得到的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;

    }

UPDATE 更新

The Url opens when i use the same proxy with Internet explorer so there must be a way.But i cannot figure it out. 当我将同一个代理与Internet Explorer一起使用时,Url打开,因此必须有一种方法。但是我无法弄清楚。

Thank you 谢谢

My guess is that the proxy blocks incoming connections of a certain nature and that is why you are running into various issues and these checks might be complex in nature or it might be as simple as setting the User-Agent to a valid browser.. I am not sure what other things a proxy can check, I would suggest you take a look at the request object created (something like Referrer, Port etc ) when you use your browser to make the request and make changes accordingly in your C# code.. 我的猜测是代理会阻止某种性质的传入连接,这就是为什么您遇到各种问题的原因,这些检查本质上可能很复杂,或者可能就像将User-Agent设置为有效的浏览器一样简单。我不确定代理还可以检查其他什么内容,建议您使用浏览器发出请求并在C#代码中进行相应更改时,查看创建的请求对象(例如Referrer,Port等)。

Good luck, let me know how it works out for you. 祝你好运,让我知道如何为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM