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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 17198|回复: 11

[HttpHelper] 求教如何模拟网页的ajax来post登陆数据?

[复制链接]
发表于 2014-9-16 09:50:20 | 显示全部楼层 |阅读模式
10金钱
闲来无事,想做个努比亚手机的查单软件,查查订单到哪里了。结果直接卡在了登陆页面。

请各位高手指点一下。


下图是抓取到的POST数据

QQ截图20140916094515.jpg



[C#] 纯文本查看 复制代码
string userName = txtUser.Text;
            string userPWD = txtPwd.Text;
            
            //跳转到登陆页面
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://account.server.nubia.cn/profile/login",//URL这里都是测试     必需项
                Method = "get"//URL     可选项 默认为Get
            };
            //得到HTML代码
            HttpResult result = http.GetHtml(item);
            StreamWriter test = new StreamWriter("D:\\test.txt");
            test.Write(result.Html);
            test.Close();

            Regex regexForm_Tag = new Regex("<input type=\"hidden\" name=\"form_tag\" id=\"form_tag\" value=\"(?<UidStr>.*?)\">", RegexOptions.Compiled);
            Match Form_Tag = regexForm_Tag.Match(result.Html);
            string Form_Tag_Str = Form_Tag.Groups[1].ToString();
            string PostStr = "form_tag=" + Form_Tag_Str + "&username=" + userName + "&password=" + userPWD + "&back_url=https%253A%252F%252Faccount.server.nubia.cn%252Fprofile%252F";
            //12ac8f360c59b7f93b08b3e6d839e690&username=yangyong_505%40163.com&password=7802505&back_url=https%253A%252F%252Faccount.server.nubia.cn%252Fprofile%252F
            item = new HttpItem()
            {
                URL = "https://account.server.nubia.cn/profile/ajax_do_login",//URL这里都是测试URl   必需项
                Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
                //Encoding = Encoding.Default,
                Method = "post",//URL     可选项 默认为Get
                Postdata = PostStr,
            };
            //得到新的HTML代码
            result = http.GetHtml(item);
            test = new StreamWriter("D:\\test.txt");
            test.Write(result.Html);
            test.Close();

            item = new HttpItem()
            {
                URL = "http://www.nubia.cn/member.php",//URL这里都是测试     必需项
                Method = "get",//URL     可选项 默认为Get
                Cookie=result.Cookie
            };
            //得到HTML代码
            result = http.GetHtml(item);
            test = new StreamWriter("D:\\test.txt");
            test.Write(result.Html);
            test.Close();


我的操作步骤是先打开登陆页面

https://account.server.nubia.cn/profile/login

从登陆页面获取POST提交所需要的form_tag值,再加上用户名,密码,来POST。

POST地址如下

https://account.server.nubia.cn/profile/ajax_do_login


可出来的结果,总是失败的,没有返回html数据。

不知道是什么原因,请各位高手指教一下。




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-9-16 10:08:53 | 显示全部楼层
  ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
加上这一行试试
回复

使用道具 举报

 楼主| 发表于 2014-9-16 10:12:45 | 显示全部楼层
站长苏飞 发表于 2014-9-16 10:08
ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
加上这一行试试

QQ图片20140916101158.jpg

还是不行啊,飞哥
回复

使用道具 举报

发表于 2014-9-16 10:16:25 | 显示全部楼层
没有数据也不表示不成功啊,你看看Cookie和Headher 是不是有数据,或者有没有跳转
回复

使用道具 举报

发表于 2014-9-16 11:13:18 | 显示全部楼层
我只是路过打酱油的。
回复

使用道具 举报

 楼主| 发表于 2014-9-16 16:01:11 | 显示全部楼层
站长苏飞 发表于 2014-9-16 10:16
没有数据也不表示不成功啊,你看看Cookie和Headher 是不是有数据,或者有没有跳转

确实是没有成功啊

提交前的Cookie
account_manage=h3990pgrrjetnjhsq01vjr5f97; path=/; domain=nubia.cn,browser_type=web; path=/; domain=.account.server.nubia.cn
提交后的Cookie
account_manage=1f8n0ng1h4v318t3if4gg59lo7; path=/; domain=nubia.cn,browser_type=web; path=/; domain=.account.server.nubia.cn

回复

使用道具 举报

发表于 2014-9-16 16:07:35 | 显示全部楼层
yy505149 发表于 2014-9-16 16:01
确实是没有成功啊

提交前的Cookie

看看Header里是什么。那就检查参数吧,肯定是少什么了,或者你提供下抓外信息看看要完整的,截图没用
回复

使用道具 举报

 楼主| 发表于 2014-9-16 16:30:59 | 显示全部楼层
站长苏飞 发表于 2014-9-16 16:07
看看Header里是什么。那就检查参数吧,肯定是少什么了,或者你提供下抓外信息看看要完整的,截图没用

test.zip (24.51 KB, 下载次数: 31)
回复

使用道具 举报

发表于 2014-9-16 17:35:33 | 显示全部楼层
Content-Type: application/x-www-form-urlencoded;

X-Requested-With: XMLHttpRequest

Referer: https://account.server.nubia.cn/profile/

DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache

把上面这些都加上,不要写的太简单,很明显的参数一定要加上,不要因为这些做不出来就闹笑话了,抓到包要详细看下。尽可能的和包一样,而不是尽可能是减少
回复

使用道具 举报

 楼主| 发表于 2014-9-17 10:33:57 | 显示全部楼层
站长苏飞 发表于 2014-9-16 17:35
Content-Type: application/x-www-form-urlencoded;

X-Requested-With: XMLHttpRequest

飞哥,我把参数全加上,还是不行啊。这倒底是为什么啊?
帮帮忙,指点一下啊。

[C#] 纯文本查看 复制代码
            //跳转到登陆页面
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://account.server.nubia.cn/profile/login",//URL这里都是测试     必需项
                Method = "GET",//URL     可选项 默认为Get
                Host="account.server.nubia.cn",
                KeepAlive=true,
                Accept = "text/html, application/xhtml+xml, */*",
                UserAgent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
                Allowautoredirect=true
            };
            //得到HTML代码
            HttpResult result = http.GetHtml(item);
            StreamWriter test = new StreamWriter("D:\\test.txt");
            test.Write(result.Cookie);
            test.Close();

            Regex regexForm_Tag = new Regex("<input type=\"hidden\" name=\"form_tag\" id=\"form_tag\" value=\"(?<UidStr>.*?)\">", RegexOptions.Compiled);
            Match Form_Tag = regexForm_Tag.Match(result.Html);
            string Form_Tag_Str = Form_Tag.Groups[1].ToString();
            string PostStr = "form_tag=" + Form_Tag_Str + "&username=" + userName + "&password=" + userPWD + "&back_url=https://account.server.nubia.cn/profile/";
            //form_tag=30b170235eca93fef7101b6e1897c04e&username=79035684%40qq.com&password=yy7802505&back_url=https://account.server.nubia.cn/profile/

            item = new HttpItem()
            {
                URL = "https://account.server.nubia.cn/profile/ajax_do_login",//URL这里都是测试URl   必需项
                Method = "POST",//URL     可选项 默认为Get
                Host="account.server.nubia.cn",
                KeepAlive=true,
                Accept="*/*",
                ContentType="application/x-www-form-urlencoded; charset=UTF-8",
                UserAgent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
                Postdata = PostStr,
                Referer = "https://account.server.nubia.cn/profile/login",
                Allowautoredirect=true
            };
            //得到新的HTML代码
            result = http.GetHtml(item);
            test = new StreamWriter("D:\\test.txt");
            test.Write(result.Cookie);
            test.Close();

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-16 08:02

© 2014-2021

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