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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 4636|回复: 8

[求助] 关于httphelper万能框架POST无法携带Postdata字符串的问题

[复制链接]
发表于 2015-9-9 09:00:10 | 显示全部楼层 |阅读模式
本帖最后由 peachp 于 2015-9-14 09:24 编辑

站长好,近期开发了自动APP的自动登陆工具,发现POST数据的时候无法携带Postdata字符串导致访问失败,请帮看看是代码的问题还是由BUG存在。
代码:
[C#] 纯文本查看 复制代码
        private void button2_Click(object sender, EventArgs e)
        {
            string c = txtCode.Text.Trim();
            string txtToPost = "code=" + c + "×tr=" + getTimeStamp() + "&sign=" + makeSign("code=" + c + "×tr=" + getTimeStamp() + "&", Sid);
            this.output(txtToPost);
            HttpItem checkin_post = new HttpItem()
            {
                URL = "http://tao.117go.com/ajax/eventMemberScoreApi/doCheckIn",
                Method = "POST",//URL     可选项 默认为Get
                Postdata = txtToPost,
                Accept = "*/*",
                ContentType = "application/x-www-form-urlencoded;charset=UTF-8",//返回类型    可选项有默认值
                UserAgent = "Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1",
                Referer = "http://tao.117go.com/event/member-score?token=" + Token + "&v=ta3.1.1&vc=117&vd=511dc319e3863c64&isontheroadinstalled=0&lat=39.882198&lng=116.471846&curLat=39.882198&curLng=116.471846&curDest=15&vts=798ef8a01c9b43cff19e0b34069e05f2",
                Host = "tao.117go.com",
                Cookie = Cookies,
                //AutoRedirectCookie = true,
                //Allowautoredirect = true
                //KeepAlive = false
            };
            checkin_post.Header.Add("Pragma", "no-cache");
            checkin_post.Header.Add("Cache-Control", "no-cache");
            checkin_post.Header.Add("Origin", "http://tao.117go.com");
            checkin_post.Header.Add("x-wap-profile", "http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml");
            checkin_post.Header.Add("Accept-Encoding", "gzip,deflate");
            checkin_post.Header.Add("Accept-Language", "zh-CN,en-US;q=0.8");
            checkin_post.Header.Add("X-Requested-With", "XMLHttpRequest");

            HttpResult checkIn = http.GetHtml(checkin_post);
            string ResponseUri = checkIn.ResponseUri;
            this.output(ResponseUri);
            Html = checkIn.Html;
            this.output(Html);
            this.output(HttpHelper.GetSmallCookie(checkIn.Cookie).ToString());
            
        }


调试时候httpitem对象的信息是对的:
1.JPG

httphelper构造抓包显示的的数据:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Origin: http://tao.117go.com
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Cookie: PHPSESSID=d6bc370c580481acd22014f56182721e;CNZZDATA1255628874=1243882827-1441460930-%7C1441460930;TZLSUID=6eddc69b284166648c1998ce4e06c78f;TZLSAPPVER=ta3.1.1;SERVERID=9d857f84e99e75ac6960a257bea59050|1441731642|1441731642
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Host: tao.117go.com
Content-Length: 75
Expect: 100-continue
Connection: Close


APP真实数据包抓包:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Host: tao.117go.com
Content-Length: 75
Pragma: no-cache
Cache-Control: no-cache
Accept: */*
Origin: http://tao.117go.com
X-Requested-With: XMLHttpRequest
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
Cookie: PHPSESSID=6e923f24463f3c6ebbafb6c763d1f202; CNZZDATA1255628874=1479652903-1441249345-%7C1441726587; TZLSUID=6eddc69b284166648c1998ce4e06c78f; TZLSAPPVER=ta3.1.1; SERVERID=29d3933615a29ae4a71ad6b0e8ddaa5a|1441727018|1441727015

code=58×tr=1441727024535&sign=e60a585226e5154f1d7a840b69852200474278a1







1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-9-9 09:55:26 | 显示全部楼层
错误提示是什么,
HTTP/1.1  这个要设置版本的
[C#] 纯文本查看 复制代码
  ProtocolVersion = System.Net.HttpVersion.Version10,//获取或设置用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11 


不会发送Post这个不用怀疑,不可能,如果这个都不管用,那还做什么http协议啊。


有问题说问题就是了。
 楼主| 发表于 2015-9-9 14:11:57 | 显示全部楼层

根据实验应该是centent-length和实际的数据长度对不上

本帖最后由 peachp 于 2015-9-9 14:16 编辑
站长苏飞 发表于 2015-9-9 09:55
错误提示是什么,
HTTP/1.1  这个要设置版本的mw_shl_code=csharp,true]  ProtocolVersion = System.Net. ...[/mw_shl_code]




系统返回:
[JavaScript] 纯文本查看 复制代码
13:57:57 {"ok":6000,"msg":"permission denied"}

根据实验应该是centent-length和实际的数据长度对不上。

正常应该返回:

[JavaScript] 纯文本查看 复制代码
{"ok":-1,"msg":"illegal request"}


根据实际测试结果:
[C#] 纯文本查看 复制代码
            
string txtToPost = "code=" + c + "×tr=" + getTimeStamp().ToString() + "&sign=" + makeSign("code=" + c + "×tr=" + getTimeStamp() + "&", Sid);
HttpItem checkin_post = new HttpItem()
            {
                URL = "http://tao.117go.com/ajax/eventMemberScoreApi/doCheckIn",
                Method = "POST",//URL     可选项 默认为Get
                Postdata = txtToPost,
                //Postdata = "code=-2×tr=1441731656124&sign=ac96a6bcf87496b73467f47243f7ce5a167e33d9",
                Accept = "*/*",
                ContentType = "application/x-www-form-urlencoded;charset=UTF-8",//返回类型    可选项有默认值
                UserAgent = "Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1",
                Referer = "http://tao.117go.com/event/member-score?token=" + Token + "&v=ta3.1.1&vc=117&vd=511dc319e3863c64&isontheroadinstalled=0&lat=39.882198&lng=116.471846&curLat=39.882198&curLng=116.471846&curDest=15&vts=798ef8a01c9b43cff19e0b34069e05f2",
                Host = "tao.117go.com",
                Cookie = Cookies,
                //ProtocolVersion = System.Net.HttpVersion.Version10
                //AutoRedirectCookie = true,
                //Allowautoredirect = true
                //KeepAlive = false
            };


以上代码中,Postdata 用string类型变量赋值就会出问题,如果用按常量赋值就没问题。
从抓包结果来看,POST请求被分成了2个TCP数据包来发送,如下图所示。
3.JPG
第一个TCP包:
[C#] 纯文本查看 复制代码
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Origin: [url=http://tao.117go.com]http://tao.117go.com[/url]
x-wap-profile: [url=http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml]http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml[/url]
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Cookie: PHPSESSID=d6bc370c580481acd22014f56182721e;CNZZDATA1255628874=1243882827-1441460930-%7C1441460930;TZLSUID=6eddc69b284166648c1998ce4e06c78f;TZLSAPPVER=ta3.1.1;SERVERID=9d857f84e99e75ac6960a257bea59050|1441731642|1441731642
Referer: [url=http://tao.117go.com/event/member-score?token=b91149bdf2bbb976f36fb1f4cc7b9d32&v=ta3.1.1&vc=117&vd=511dc319e3863c64&isontheroadinstalled=0&lat=39.882198&lng=116.471846&curLat=39.882198&curLng=116.471846&curDest=15&vts=798ef8a01c9b43cff19e0b34069e05f2]http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2[/url]
Host: tao.117go.com
Content-Length: 75
Expect: 100-continue
Connection: Close

第二个TCP包:
[C#] 纯文本查看 复制代码
code=-2×tr=1441731656124&sign=ac96a6bcf87496b73467f47243f7ce5a167e33d9


从现象看应该是服务器收到第一个包以后发现content和content-length不符,直接就返回出错信息了。

站长啊,这个咋解决?





发表于 2015-9-9 14:17:50 | 显示全部楼层
那为什么要发两次呢,你看下你的代码是不是有地方发送了两次,我的类执行一次只会提交一次的。

那就看一下用变量的时候获取的PostData串和常量的时候是不是一样的。这个你调试看看,如果是一样的,不可能结果不一样。这个理论上都不通。一定是其他代码影响了,或者是说你未贴出的代码影响了。

不能发送Post数据这点你真不用怀疑,如这点我都做不到,我还做什么Httphelper类啊。这是最基础的东西。真有这个Bug,我想也会等你发现,呵呵,我自己就发现了。
 楼主| 发表于 2015-9-9 14:35:33 | 显示全部楼层
站长苏飞 发表于 2015-9-9 14:17
那为什么要发两次呢,你看下你的代码是不是有地方发送了两次,我的类执行一次只会提交一次的。

那就看一 ...

站长我没有质疑httphelper的意思,我之前也阅读过代码,也没看出问题,所以有此问。这个工具我认为还是很实用的,主要是开发遇到了疑难的问题,各种途径都测试过了,需要您这样的高手帮帮帮忙。

经过代码对比,我发现Postdata动态赋值时候,最后会默认加一个Connection: Close,而静态赋值不会,能否帮看看这个Connection: Close是怎么加上的,能否去掉呢?

动态赋值:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Origin: http://tao.117go.com
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Cookie: PHPSESSID=d6bc370c580481acd22014f56182721e;CNZZDATA1255628874=1243882827-1441460930-%7C1441460930;TZLSUID=6eddc69b284166648c1998ce4e06c78f;TZLSAPPVER=ta3.1.1;SERVERID=9d857f84e99e75ac6960a257bea59050|1441731642|1441731642
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Host: tao.117go.com
Content-Length: 75
Expect: 100-continue
Connection: Close

code=-2×tr=1441731656124&sign=ac96a6bcf87496b73467f47243f7ce5a167e33d9

静态赋值:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Origin: http://tao.117go.com
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Cookie: PHPSESSID=531836e592d2f65563d1bbcd51a195a9;CNZZDATA1255628874=1243882827-1441460930-%7C1441460930;TZLSUID=6eddc69b284166648c1998ce4e06c78f;TZLSAPPVER=ta3.1.1;SERVERID=ca84f51d98e443c428682e33db834273|1441779857|1441779857
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Host: tao.117go.com
Content-Length: 75
Expect: 100-continue

code=-2×tr=1441731656124&sign=ac96a6bcf87496b73467f47243f7ce5a167e33d9
发表于 2015-9-9 15:21:12 | 显示全部楼层
ProtocolVersion = System.Net.HttpVersion.Version10,//获取或设置用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11  加上这一句也不行吗?
 楼主| 发表于 2015-9-14 00:59:28 | 显示全部楼层
本帖最后由 peachp 于 2015-9-14 10:13 编辑

站长好,之前的问题已经解决,通过调整Expect100Continue=false参数就可以避免出现分包等待的情况。不过出现了新问题,还是先在这里问一下,如果不符合论坛规定我再开新贴。

 楼主| 发表于 2015-9-14 10:11:45 | 显示全部楼层
站长苏飞 发表于 2015-9-9 15:21
ProtocolVersion = System.Net.HttpVersion.Version10,//获取或设置用于请求的 HTTP 版本。默认为 System.N ...

站长好,之前的问题已经解决,通过调整Expect100Continue=false参数就可以避免出现分包等待的情况。不过出现了新问题,还是先在这里问一下,如果不符合论坛规定我再开新贴。
经过反复修改我的程序目前已经能够构造出HEAD和内容和真实数据一样的数据包,经过对比发现只有HEAD中各个字段的顺序不同,请问httphelper框架能够修改数据包HEAD字段的顺序么?如果需要编辑原代码能否告知修改那一个文件中的哪个函数,万分感谢!
我构造的数据包:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Origin: http://tao.117go.com
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Cookie: PHPSESSID=772a23ae207365f85bccc9d007a44628;TZLSUID=6eddc69b284166648c1998ce4e06c78f;TZLSAPPVER=ta3.1.1;SERVERID=ceda2523bfa4d3b1e9996ae274e8fe9c|1442162638|1442162638;CNZZDATA1255628874=1479652903-1441249345-%7C1442148263
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Host: tao.117go.com
Content-Length: 76

code=-12&timestr=1442162646487&sign=694cba3565926f358b05303fb8691ae8ef892fcc


真实数据包:
POST /ajax/eventMemberScoreApi/doCheckIn HTTP/1.1
Host: tao.117go.com
Content-Length: 75
Pragma: no-cache
Cache-Control: no-cache
Accept: */*
Origin: http://tao.117go.com
X-Requested-With: XMLHttpRequest
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_MT7-TL00_UAProfile.xml
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 taoontheroad-a3.1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://tao.117go.com/event/membe ... 3cff19e0b34069e05f2
Accept-Encoding: gzip,deflate
Accept-Language: zh-CN,en-US;q=0.8
Cookie: PHPSESSID=6e923f24463f3c6ebbafb6c763d1f202; TZLSUID=6eddc69b284166648c1998ce4e06c78f; TZLSAPPVER=ta3.1.1; SERVERID=29d3933615a29ae4a71ad6b0e8ddaa5a|1442152706|1442152700; CNZZDATA1255628874=1479652903-1441249345-%7C1442148263

code=36&timestr=1442152712438&sign=eed79d503d2f5a10541fcd32983d7ff6503d465d
发表于 2015-9-14 10:24:27 | 显示全部楼层
peachp 发表于 2015-9-14 10:11
站长好,之前的问题已经解决,通过调整Expect100Continue=false参数就可以避免出现分包等待的情况。不过 ...

参数的顺序是和你录入的一样的。我的框架没有干涉
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-3-29 21:37

© 2014-2021

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