【发布时间】: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