本帖最后由 piaoye 于 2015-3-29 21:47 编辑
这跟asp.net的后台机制有关,将__VIEWSTATE和__EVENTVALIDATION两个隐藏表单以及按钮名称作为Postdata的一部分一起提交即可,注意用 UrlEncode编码
[C#] 纯文本查看 复制代码 HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://www.bszb.cn/ezb/cx.aspx",//URL 必需项
Method = "get"//URL 可选项 默认为Get
};
HttpResult result = http.GetHtml(item);
string vstate = Regex.Match(result.Html, "<input type=\"hidden\" *.*id=\"__VIEWSTATE\" * .*value=\"(.+)?\" *\\/>", RegexOptions.IgnoreCase).Groups[1].Value;
string evalidation = Regex.Match(result.Html, "<input type=\"hidden\" *.*id=\"__EVENTVALIDATION\" * .*value=\"(.+)?\" *\\/>", RegexOptions.IgnoreCase).Groups[1].Value;
item = new HttpItem()
{
URL = "http://www.bszb.cn/ezb/getVCode.aspx",//URL 必需项
Method = "get"//URL 可选项 默认为Get
};
result = http.GetHtml(item);
string code = Regex.Match(result.Html, "(\\d{5,8})", RegexOptions.IgnoreCase).Groups[1].Value;
item = new HttpItem()
{
URL = "http://www.bszb.cn/ezb/cx.aspx",//URL 必需项
Method = "post",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = result.Cookie.Replace("path=/; HttpOnly", string.Empty).Replace(";",string.Empty),//字符串Cookie 可选项
Postdata = string.Format("txtNo={0}&txtCode={1}&__VIEWSTATE={2}&__EVENTVALIDATION={3}&Button1=Button1", "WZT150000045", code,HttpUtility.UrlEncode(vstate),HttpUtility.UrlEncode(evalidation)),
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Expect100Continue=false,
ProtocolVersion=HttpVersion.Version11
};
result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
|