|
本帖最后由 tylrr 于 2014-7-3 15:30 编辑
我使用webbrowser打开百度的推广后台登陆页面后,手动输入账号登陆,然后获取了登陆后的cookie,然后把cookie值传递给httpHelper,提交post请求去查询。
但是不知道什么原因,老是出错,提示:"Unable to connect to the remote server" 。
网络是正常的,请问是哪里出错呢? 谢谢指点
首先使用webbrowser控件加载登陆页面,手动输入账号和密码登陆后,获取到登陆后的cookie。然后把cookie赋值到httphelper进行查询。
private void test1()
{
string tk = "";
string tk2 = "";
string mycookie = "";
string pattern = "(?<=cas__st__=).*?(?=;)";
MatchCollection collection = Regex.Matches(MainCookie.Cookie, pattern);
foreach (Match match in collection)
{
tk = match.ToString(); //获取token1
}
pattern = ".{10}";
collection = Regex.Matches(tk, pattern);
foreach (Match match in collection)
{
tk2 = match.ToString(); //获取token2
}
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://fengchao.baidu.com/nirvana/request.ajax?path=GET/kr/word&reqid=lig0s75s0m",//URL 必需项
Method = "post",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = MainCookie.Cookie, //获取到登陆后的cookie
Referer = "http://fengchao.baidu.com/nirvana/main.html?userid=5599168&t=1404351595401#/manage/plan",//来源URL 可选项
Postdata = "eventId=&path=GET%2Fkr%2Fword&userid=5599168&token=" + tk + "¶ms={\"logid\":982890698,\"planid\":0,\"unitid\":0,\"entry\":\"kr_tools\",\"query\":\"test\",\"regions\":\"\",\"rgfilter\":1,\"device\":0,\"querytype\":1,\"querySessions\":[\"test\",\"心理\",\"测试\"]}",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded; charset=utf-8",//返回类型 可选项有默认值
Allowautoredirect = true,//是否根据301跳转 可选项
ResultType = ResultType.String
};
HttpResult result = http.GetHtml(item); //根据抓包信息进行第1次查询
mycookie = result.Cookie;
Debug.Write("cookie:"+MainCookie.Cookie+"\r\n");
HttpItem item2 = new HttpItem();
item2.URL = "http://fclog.baidu.com/nirvana/log/fclogimg.gif";//URL 必需项
item2.Method = "post";//URL 可选项 默认为Get
item2.IsToLower = false;//得到的HTML代码是否转成小写 可选项默认转小写
item2.Cookie = mycookie;//字符串Cookie 可选项
item2.Referer = "http://fengchao.baidu.com/nirvana/main.html?userid=5599168&t=1404351595401";//来源URL 可选项
item2.Postdata = "userid=5599168&optid=5599168&ulevelid=10101&token=" + tk2 + "&exp=undefined&path=%252Fmanage%252Fplan&fn=query&logid=982890698&entry=kr_tools&krType=1&query=test®ion=&rgfilter=1&isResearch=1&krCol=word%252Ctotal_weight%252Cpv%252Ckwc&lastTarget=queryResponse";//Post数据 可选项GET时不需要写
item2.Timeout = 100000;//连接超时时间 可选项默认为100000
item2.ReadWriteTimeout = 30000;//写入Post数据超时时间 可选项默认为30000
item2.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";//用户的浏览器类型,版本,操作系统 可选项有默认值
item2.ContentType = "application/x-www-form-urlencoded; charset=utf-8";//返回类型 可选项有默认值
item2.Allowautoredirect = true;//是否根据301跳转 可选项
item2.ResultType = ResultType.String;
result = http.GetHtml(item2); //根据抓包信息进行第2次查询
string html = result.Html;
MessageBox.Show(html);
mycookie = result.Cookie;
}
|
|