当我在HttpwebRequest c#中添加Host时,代理不工作
本文关键字:代理 工作 Host 添加 HttpwebRequest | 更新日期: 2023-09-27 18:01:01
我正试图使用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;
您能分享更多的细节吗,比如如何创建请求、设置代理等?通常情况下,您不必像那样明确地设置主机。
这是我拥有的一份代码副本,它可以与代理完美配合:
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";