本帖最后由 CrazyTdkgn 于 2014-10-16 12:38 编辑
鄙人学生狗,想编个程序从学校网站爬取所有本校学生的基本个人信息,但是处理一个post请求遇到了问题,求各位大神指点。
如图这么一个局部页面,当我输入学号并点击查询的时候,能用IE捕获一个HTTP请求
用学号20131002做测试的时候,捕获的请求正文如下
__VIEWSTATE=HLl7RXFL6YpOPaz9Ks92d5pfyHlcO9h4M1myiZ4CDMijHAADaC%2FClM8CawE2W%2F9dJNN2HG%2FQnZ5k0dW6HM%2FE%2Bn%2BMAosdT%2Bst1MsJsCTHu4o9qs5K%2BaiygQWBsoM1bcGTWq7iWnQoo%2FTc1FsnVSIbTXlAhhf%2B2U%2BKGkE1vpxDMFCyxFNHQb1XWidAYRmgxq7Yef5dYhEU4vd%2Fyt8KJ00WWuWNgTIDg%2BmIpO%2FK9KiRz%2Fes1VUpSJQrrVIrVnlmmQKA5n3yrdt3EPXk%2FHIt44TD%2BmNBglXZ5aaRNt9LXrlgNfYeXHEmSWDGNI29sJXc1iMBOjFu%2BtV62oyqnb%2FKLoOEFJz0tvvS2vZ57kTeUn596Krhilr2Y0WKk1DSHJ10xpVS5h5SYWRidzligH7f%2BV001UVjhopBxQMplPLfYp%2BoLCKV5rv%2FzR7RvqPk6v5TxhARrSZj5yqXcC6mZ2OhlK1UDSaWysLENT8BNwWa1rJiA7VOwicCo9YVeBl%2BkkuyzKlITCvthRfdTRoF1QIV2Zmxp%2F1qqpRVhOWdhXxXlRPes9h4FvbQfWXShdma3VHfHIAciXSCNZCC6OBu7VlZesIWF8b7wo%2Fa3BU7el6skfal3qlR%2BVNrjBtkS0aSCyQFsXNpJshwmC3J1X%2FxnsNiA67igLSSIMvy41rN%2Fe%2B1NK4reWHQzmHG0siqZKOVOcsgiFDr0STS4byuGkVd95mlndyVR3rZoiZj8wFMI2X%2BLqd6iQ2OvGKrH7FwLkS%2Fa3E%2BYsuhkduQCH91y385AMiLwfUdOSc%2Fpu8mGT2U4mnaNwwpd68lwrD3&__VIEWSTATEGENERATOR=E08963AD&__VIEWSTATEENCRYPTED=&__EVENTVALIDATION=Ceyz650IL4OhDS0syr0KXWxq4V7p5ovghORzJ7hpYr3IdszhfCwfS9AJnnGwMbNOp5fGgCRX5uTUqrXVsVCHe7gb9NfLIsY8ZIOo3z894NI0%2Fr4C&StudentNo=20131002&StudentName=&Button1=%B2%E9%D1%AF&SearchStyleControl_currentState=False
我的post实现主体贴在下面
[C#] 纯文本查看 复制代码 public string HttpPost(string Url,string strPostdata)
{
StringBuilder content = new StringBuilder();
try
{
// 与指定URL创建HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.Method = "POST";
request.Accept = "*/*";
//出于隐私考虑,cookie实际内容“键”、“值”代替
CookieContainer objcok = new CookieContainer();
objcok.Add(new Uri("http://www.yanghua.net/SystemForm/Student/Search/StuInfoSearch.aspx"), new Cookie("键", "值"));
objcok.Add(new Uri("http://www.yanghua.net/SystemForm/Student/Search/StuInfoSearch.aspx"), new Cookie("键", "值"));
objcok.Add(new Uri("http://www.yanghua.net/SystemForm/Student/Search/StuInfoSearch.aspx"), new Cookie("键", "值"));
request.CookieContainer = objcok;
//不保持连接
request.KeepAlive = true;
byte[] buffer = Encoding.Default.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
// 获取对应HTTP请求的响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取响应流
Stream responseStream = response.GetResponseStream();
// 对接响应流(以"GBK"字符集)
StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
// 开始读取数据
Char[] sReaderBuffer = new Char[256];
int count = sReader.Read(sReaderBuffer, 0, 256);
while (count > 0)
{
String tempStr = new String(sReaderBuffer, 0, count);
content.Append(tempStr);
count = sReader.Read(sReaderBuffer, 0, 256);
}
// 读取结束
sReader.Close();
}
catch (Exception)
{
content = new StringBuilder("Runtime Error");
}
return content.ToString();
}
但是我把上述请求正文作为postdata传入的时候,得到的响应却不是预期的响应。
预期响应片段:
实际响应片段:
不知道为什么会未找到相关数据。
不知道我有没有把我的问题描述清楚了。如果还需要其他信息,我再提供。希望大神们能给一个解决方案,已经卡了两天了。谨拜谢!
|