http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

12
返回列表 发新帖
楼主: xz_whist

[其他] 郁闷呀,post登录成功后,并未保持登录状态

[复制链接]
 楼主| 发表于 2015-3-26 21:15:42 | 显示全部楼层
站长苏飞 发表于 2015-3-25 09:00
你应该看看http://bbs.feng.com/api/uc.php?time=1427244953&code=58f1tITw10qtzFGgjn%2Bg9FqvVRiZVzIdM4Zi ...

今天测试了下,在登陆成功后,得到返回的url,就是 http://bbs.feng.com/api 那个,然后在去get,得到的cookie,里面包含了 你说的3个cookie值,再去get威锋主页 bbs.feng.com 还是没有保存住登陆的状态,站长可不可以帮忙搞个demo呀


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
回复

使用道具 举报

发表于 2015-3-27 08:16:59 | 显示全部楼层
3个cookie值从那里得到的。贴下代码。得到了就肯定能登录
回复

使用道具 举报

 楼主| 发表于 2015-3-27 19:22:28 | 显示全部楼层
本帖最后由 xz_whist 于 2015-3-27 19:25 编辑
站长苏飞 发表于 2015-3-27 08:16
3个cookie值从那里得到的。贴下代码。得到了就肯定能登录

第一步、初始化
[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里面 还是没有登陆

        }





回复

使用道具 举报

 楼主| 发表于 2015-3-29 14:43:33 | 显示全部楼层
站长苏飞 发表于 2015-3-27 08:16
3个cookie值从那里得到的。贴下代码。得到了就肯定能登录

好了,站长,终于搞定了,谢谢你的提示
回复

使用道具 举报

发表于 2015-3-30 08:17:24 | 显示全部楼层
xz_whist 发表于 2015-3-29 14:43
好了,站长,终于搞定了,谢谢你的提示

哦那恭喜了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-24 08:44

© 2014-2021

快速回复 返回顶部 返回列表