【问题标题】:Proxy not working when i add Host in HttpwebRequest c#当我在 HttpwebRequest c# 中添加主机时代理不起作用
【发布时间】:2014-05-01 13:15:11
【问题描述】:

我正在尝试使用HttpWebRequest Post 登录网站。它工作得很好。但是当我使用代理登录时,它不会工作。连接超时,如果我删除部分:

request.Host="abc.com";

它与代理再次正常工作,但执行上述操作将禁止我登录,因为该站点需要该信息。我怎样才能提出任何建议?

代码”

                HttpWebRequest httpWReq =
               (HttpWebRequest)WebRequest.Create(url);

                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = post;
                byte[] data = encoding.GetBytes(postData);

                string proxy = "58.20.127.26:3128";
               /////byte[] data = GetBytes(postData);
                WebProxy myProxy = new WebProxy(proxy);
                httpWReq.Proxy = myProxy;

                httpWReq.Method = "POST";
                httpWReq.Accept = "text/html, application/xhtml+xml, */*";
                httpWReq.Referer = refferr;
                httpWReq.CookieContainer = yumCookies;
                httpWReq.ContentType = "application/x-www-form-urlencoded";
                httpWReq.UserAgent = "xxxxxxxxxxx";
                httpWReq.Host = "xxx.com";
                httpWReq.Headers.Add("Accept-Language: en-US");
                httpWReq.ContentLength = data.Length;
                httpWReq.KeepAlive = true;
                httpWReq.Headers.Add("Pragma: no-cache");
                httpWReq.AllowAutoRedirect = true;
                httpWReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(responseStream);
                    responseString = myStreamReader.ReadToEnd();

                    if (response.Cookies.Count > 0)
                    {
                        foreach (Cookie ck in response.Cookies)
                        {
                            yumCookies.Add(ck);

                        }
                    }
                }
                response.Close();

                response = null;
                response = null;

【问题讨论】:

    标签: c# httpwebrequest web-scraping


    【解决方案1】:

    您能否分享更多细节,例如您如何创建请求、设置代理等?通常你不必像那样显式设置主机。

    这是我拥有的代码的副本,可以完美地与代理配合使用:

                        var req = WebRequest.Create("http://google.com") as HttpWebRequest;
                        req.Proxy = new WebProxy("proxy-ip", 8080); //proxy ip/port
                        req.ContentType = "text/html";
                        req.Method = "GET";
    

    【讨论】:

    • 是的,如果我删除主机部分,它对我和我的也是如此。您可以添加 req.Host='google.com' 然后检查它是否适用于代理。
    • 尝试在标头中设置主机,或尝试另一个代理,因为它可能无法正确传递标头
    • 我尝试过很多次的每个代理都会发生同样的情况,如果我在标头中发送主机,它会给出错误,就像它不是可以像上面那样创建的地方。
    • 我已经看到了,这对我不好,因为我需要定期更改 ip,这类似于将 ip 放入 IE 中,并且也可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2018-08-14
    • 2021-03-22
    相关资源
    最近更新 更多