本帖最后由 xz_whist 于 2015-3-27 19:25 编辑
第一步、初始化
[C#] 纯文本查看 复制代码
//先预先加载,然后再登陆
HttpItemObj = new HttpItem();
HttpResultObj = new HttpResult();
HttpHelperObj = new HttpHelper();
HttpItemObj.Method = "GET";
HttpItemObj.URL = "http://passport.feng.com/?r=user/login&sso%5Bjump%5D=http%3A%2F%2Fbbs.feng.com%2F&sso%5Bname%5D=%E5%A8%81%E9%94%8B%E8%AE%BA%E5%9D%9B&sso%5Bapps%5D=2&sso%5Bverify%5D=7a928982dd59b02a3024858bd61ccf22";
HttpItemObj.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
HttpItemObj.Allowautoredirect = true;
HttpResultObj = HttpHelperObj.GetHtml(HttpItemObj);
cookies = HttpResultObj.Cookie;
string htmltext = HttpResultObj.Html;
this.txtLog.Text = htmltext;
//得到返回的网页源码,从源码中获取 登陆要用到的元素
strV_token = HtmlHelper.GetWfLoginV_token(htmltext);
第二步,根据第一步的cookie ,去登陆
[C#] 纯文本查看 复制代码 HttpItemObj = new HttpItem();
HttpItemObj.URL = string.Format("http://passport.feng.com/index.php?r=user/LoginProcess");
HttpItemObj.Method = "POST";
HttpItemObj.Allowautoredirect = true;
HttpItemObj.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
//Post参数,动态密保,密码,用户名,v_token
HttpItemObj.Postdata = string.Format("check_code={0}&password={1}&username={2}&v_token={3}&wekey_token=", string.Empty, this.txtPwd.Text.Trim(), this.txtName.Text.Trim(),strV_token);
HttpItemObj.Header.Add("X-Requested-With", "XMLHttpRequest");
HttpItemObj.Header.Add("Accept-Encoding", "gzip, deflate");
HttpItemObj.Referer = "http://passport.feng.com/?r=user/login&sso%5Bjump%5D=http%3A%2F%2Fbbs.feng.com%2F&sso%5Bname%5D=%E5%A8%81%E9%94%8B%E8%AE%BA%E5%9D%9B&sso%5Bapps%5D=2&sso%5Bverify%5D=7a928982dd59b02a3024858bd61ccf22";
HttpItemObj.Accept = "application/json, text/javascript, */*; q=0.01";
HttpItemObj.Cookie = cookies;
HttpResultObj = HttpHelperObj.GetHtml(HttpItemObj);
//本次返回的cookie已经包含了之前cookie,所以不叠加,直接替换
cookies = HttpResultObj.Cookie;
string strResult = HttpResultObj.Html.Replace("\r\n","").Trim();
WfLogin WfLoginObj = new WfLogin();
WfLogin obj = (WfLogin)JsonHelper.JsonToObject(strResult, WfLoginObj);
if (obj.status != null && obj.status == "success")
{
labLoginStatus.Text = string.Format("登陆状态:已登陆");
//根据此url再次get
//获取到登陆后返回的Json信息,去解析
//{"status":"success",
//"info":
//{"name":"\u5a01\u950b\u7f51\u8bba\u575b",
//"link":"http:\/\/bbs.feng.com\/","wait":2000,
//"syn_str":"<script type=\"text\/javascript\"
//src=\"http:\/\/bbs.feng.com\/api\/uc.php?time=1427012845&code=0662KtsBn9%2FKGk2M30eCNiqDIlYz0XLtlp0Ib07yizeKT8I0N%2Fw%2BB3Y%2FIsSvEqTKY3HXfkgX8mZvlG4%2FXEPCQLOyrH6pQ%2Fr22SsBth2%2FoJ5epwfP9HrKwX00VA5O5PjBog6u6mYpy0WYwc697OOnJe72FAsk8Z4K3AR7pse9KsNgb9RLIHso5lT5iePEfJZC%2FG4%2BfZSYwYAuVDqUX8NXClsqIz17ajWSic6lKAeyGrVUZHEtf6tF%2FfpBiKaB8%2BXglCZNrIqVsXpEFgJxBDbuoXzLADIRjw92Lm4uRm%2Fd2gV0JTc4BatB9LqeGq4f8Us46Karj0zXSkVnN4HVHYdIuXfscg4OuR9lAX0c26EZcUuuXC5HPmt5PsW51tS6IRpORovjflESLga%2FMC3Ixv88hF9V1wCpYEz6sDbWQkyqpu%2BTXouUObW3Sld2Vim9wiiGWMnQnhqCaRcS\" reload=\"1\"><\/script>"}}
strLoginUrl = HtmlHelper.GetWfLoginResutUrl(obj.info.syn_str);
GetLoingResultUrl(strLoginUrl);
}
else
{
//登陆失败的 错误提示
labLoginStatus.Text = string.Format("登陆状态:登陆失败");
}
第三部,登陆成功返回的url 再次获取cookie,也就是 GetLoingResultUrl(strLoginUrl); 方法
[C#] 纯文本查看 复制代码 public void GetLoingResultUrl(string strUlr)
{
HttpItemObj = new HttpItem();
HttpItemObj.Method = "GET";
HttpItemObj.URL = string.Format(strUlr);
HttpItemObj.Referer = "http://passport.feng.com/?r=user/login&sso%5Bjump%5D=http%3A%2F%2Fbbs.feng.com%2F&sso%5Bname%5D=%E5%A8%81%E9%94%8B%E8%AE%BA%E5%9D%9B&sso%5Bapps%5D=2&sso%5Bverify%5D=7a928982dd59b02a3024858bd61ccf22";
HttpItemObj.Allowautoredirect = true;
HttpItemObj.Accept = "text/html, application/xhtml+xml, */*";
HttpItemObj.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
HttpItemObj.Header.Add("X-Requested-With", "XMLHttpRequest");
HttpItemObj.Header.Add("Accept-Encoding", "gzip, deflate");
//string str = string.Format("CNZZDATA30092183=cnzz_eid=290271446-1427012417-&ntime=1427012417");
HttpItemObj.Cookie = cookies;
HttpResultObj = HttpHelperObj.GetHtml(HttpItemObj);
//这步骤的cookie 就得到了登陆所用到的cookie
cookies = cookies + "," + HttpResultObj.Cookie;
//然后再去get bbs.feng.com 返回的html里面 还是没有登陆
}
|