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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 11177|回复: 14

[HttpHelper] 12306 登陆问题

[复制链接]
发表于 2014-8-5 10:09:34 | 显示全部楼层 |阅读模式
1金钱
首先说下我的思路,不知道正确不正确……(因为我刚接触这一方面,有什么逗的地方 还请大家多多见谅,谢了)
1.通过get请求获取到一个验证码图片,并记录LoginBeforeCookie.
[C#] 纯文本查看 复制代码
    
    public Image getcodeimg()
        {
            http = new HttpHelper();
            item = new HttpItem()
            {
                URL = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
                ResultType = ResultType.Byte
            };
            HttpResult result = http.GetHtml(item);
            LoginBeforeCookie= result.Cookie;
            Image code = byteArrayToImage(result.ResultByte);
            return code;
        }

2.通过Post请求将账号和密码以及验证码传入进去(当然Cookie也会带上)。
[C#] 纯文本查看 复制代码
 
       public string Login(string user, string pwd, string randcode)
        {
            http = new HttpHelper();
            item = new HttpItem()
           {
               URL = "https://kyfw.12306.cn/otn/login/loginAysnSuggest",//URL     必需项  
               Method = "post",//URL     可选项 默认为Get   
               IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
               Cookie = LoginBeforeCookie,
               UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
               ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
               Postdata = "loginUserDTO.user_name="+user+"&userDTO.password=“+pwd+”&randCode=" + randcode + ""
           };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            return html;
        }


可是结果一般是两种情况:
    一:验证码错误。{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{},"messages":["验证码错误"],"validateMessages":{}}(验证码肯定是填写的图片的内容,这个不会手抖,我自己代码一般爆这个错)
    二:网络忙,请刷新重试!{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{},"messages":["网络忙,请刷新重试!"],"validateMessages":{}}(苏飞开发助手一般爆这个错)

   求指导思路!!!!!



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2014-8-5 10:10:42 | 显示全部楼层
沙发自己坐!!!
回复

使用道具 举报

 楼主| 发表于 2014-8-5 10:35:57 | 显示全部楼层
已经解决了!不能用字符串的cookie  必须使用CookieCollection!!~~~哈哈 不知道可以自己给自己结贴不~~
回复

使用道具 举报

发表于 2014-8-5 10:42:20 | 显示全部楼层
板凳我帮你坐。
回复

使用道具 举报

 楼主| 发表于 2014-8-5 10:58:53 | 显示全部楼层
自己已经解决了~~可是回复为什么没效果呢~~~不能用Cookie字符串 只能使用CookieContainers!
回复

使用道具 举报

发表于 2014-8-5 19:23:46 | 显示全部楼层
帖子是不能给你自己的,呵呵。
回复

使用道具 举报

 楼主| 发表于 2014-8-6 09:25:52 | 显示全部楼层
站长苏飞 发表于 2014-8-5 19:23
帖子是不能给你自己的,呵呵。

飞哥 我想问一下 同样的这样一段代码,为什么返回的值不一样呢?

[C#] 纯文本查看 复制代码
     public string Login(string user, string pwd, string randcode)
        {
            http = new HttpHelper();
            item = new HttpItem()
           {
               URL = "https://kyfw.12306.cn/otn/login/loginAysnSuggest",//URL     必需项  
               Method = "post",//URL     可选项 默认为Get   
               IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
               UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
               ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
               Postdata = "loginUserDTO.user_name=******&userDTO.password=*********&randCode=" + randcode + "",
               ResultCookieType = ResultCookieType.CookieCollection,
               CookieCollection = HttpCookie
           };
            HttpResult result = http.GetHtml(item);
            HttpCookie.Add(result.CookieCollection);
            string html = result.Html;
            return html;
        }


{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"loginAddress":"10.1.212.51","loginCheck":"Y"},"messages":[],"validateMessages":{}}(反正这个可以进行后面的操作,并且有结果返回)

{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"loginCheck":"Y"},"messages":[],"validateMessages":{}}(但是返回这个的话 后面的操作就没有数据返回了)

刚接触这个,感觉好复杂啊~ 求飞哥帮忙解答一下。
回复

使用道具 举报

发表于 2014-8-6 09:33:06 | 显示全部楼层
你是说两次请求值不是一样吗?
回复

使用道具 举报

 楼主| 发表于 2014-8-6 09:36:29 | 显示全部楼层
站长苏飞 发表于 2014-8-6 09:33
你是说两次请求值不是一样吗?

请求值的验证码是不一样的! 其他的都一样!因为我在调试间断,所以会平凡的走这个流程,有时是出现上面的答案,大多数是出现下面的答案!
回复

使用道具 举报

发表于 2014-8-6 09:38:27 | 显示全部楼层
w393278126 发表于 2014-8-6 09:36
请求值的验证码是不一样的! 其他的都一样!因为我在调试间断,所以会平凡的走这个流程,有时是出现上面 ...

这个不好说,呵呵。建议你使用Http的连接而不要使用Https参考这里http://www.sufeinet.com/thread-4270-1-1.html
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 10:08

© 2014-2021

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