我在抓取https页面时遇到了HttpWebRequest的问题 [英] I have a problem with HttpWebRequest in grab https pages
问题描述
   这个问题让我困扰了一个星期,我对此一无所知。
Hi,the problem troubled me a week, and I haven't a clue on it.
我使用HttpWebRequest登录网站。首先,我应该在普通页面中输入一些文本框,然后重定向到登录页面,这是一个https网址,但是HttpWebRequest无法访问登录页面,
会返回会话超时页面。我使用HttpWebRequest标头添加了postdata和cookie,但它不起作用。
I use the HttpWebRequest to login a website. First I should input some textbox in the normal page and then redirect to the login page which is a https url,but the HttpWebRequest couldn't get access to the login page and return a session time out page. I add the postdata and cookie with the HttpWebRequest header ,but it's not work.
此外,当我使用python做同样的事情时,它的工作原理!我不知道为什么HttpWebRequest无法工作。
Besides, when I use python to do the same thing,it works! I don't know why the HttpWebRequest couldn't work.
这是代码:
Here's the code:
//hear I set the validation callback returns true
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://login.xxx.com");
request.MaximumAutomaticRedirections = 10;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.Proxy = null;
request.MaximumAutomaticRedirections = 10;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.Proxy = null;
request.Headers.Add("Accept", "*/*");
request.Headers.Add("Accept-Language", "zh-cn");
request.Headers.Add("UA-CPU", "x86");
request.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
request.Headers.Add("Connection", "Keep-Alive");
//here the cookies contains all the cookies I get from the previous request and response
request.Headers.Add("Cookie", "");
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
//here I add the post data to the request
System.IO.Stream stream = null;
try
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(post);
request.ContentLength = buffer.Length;
stream = request.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
}
catch
{
}
finally
{
if (stream != null)
{
stream.Close();
}
}
//I get the response and the response is a time out ResponsePage
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
推荐答案
感谢您的发帖! 我建议将你的问题发布在MS论坛之一,
这篇关于我在抓取https页面时遇到了HttpWebRequest的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!