简体   繁体   English

如何使用python机械化发送有效载荷

[英]how to send payload with python mechanize

I want to get info from internet, I use python mechanize modules to do this, But it need post payload while request, I trid many ways but still failed. 我想从互联网上获取信息,我使用python机械化模块来执行此操作,但是在请求时需要发布有效负载,我尝试了多种方法,但仍然失败。 please help me. 请帮我。
here is the post data i grap: 这是我掌握的帖子数据:

Request 请求

URL:xxxxxx 网址:xxxxxx
Request Method:POST 请求方法:POST
Status Code:200 OK 状态码:200 OK

Request Header 请求标题

**Accept:application/json, text/javascript, */*; q=0.01**  
Accept-Charset:gb18030,utf-8;q=0.7,*;q=0.3  
Accept-Encoding:gzip,deflate,sdch  
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4  
Connection:keep-alive  
Content-Length:52  
**Content-Type:application/json; charset=UTF-8**  
Cookie:ASP.NET_SessionId: sanntewiq5cz5uq1y0l5g3gy  
Host:xxx.xxx.xxx.xxx:8500  
Origin:xxx.xxx.xxx.xxx:8500  
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko)  Chrome/24.0.1312.57 Safari/537.17  
**X-Requested-With:XMLHttpRequest**

Request Payload 请求有效载荷

{dispatchorderid:"966A48E572624F2FB2E99F371C232729"} {dispatchorderid:“ 966A48E572624F2FB2E99F371C232729”}

here is my code: 这是我的代码:

br = mechanize.Browser()
payload = {'dispatchorderid': "966A48E572624F2FB2E99F371C232729"}
json_data = json.dumps(payload)
br.addheaders = [('Content-Type', 'application/json; charset=UTF-8'),
                ('Accept', 'application/json, text/javascript, */*; q=0.01'),
                ('X-Requested-With', 'XMLHttpRequest')]
br.set_debug_http(True)
res = br.open(url, json_data)

when i run this script, the send info is: 当我运行此脚本时,发送信息为:

send: 'POST /DealerManage/AddDispatchorder.aspx/GetDispatchorderEntityList HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nContent-Length: 55 发送:'POST /DealerManage/AddDispatchorder.aspx/GetDispatchorderEntityList HTTP / 1.1 \\ r \\ nAccept-Encoding:identity \\ r \\ n内容长度:55
Connection: close 连接方式:关闭
Accept: application/json, text/javascript, / ; 接受:application / json,text / javascript, / q=0.01 q = 0.01
Host: 125.64.15.71:8500 主持人:125.64.15.71:8500
Cookie: ASP.NET_SessionId=34dyw50d3omel2vbsvm41zwp Cookie:ASP.NET_SessionId = 34dyw50d3omel2vbsvm41zwp
X-Requested-With: XMLHttpRequest X-Requested-With:XMLHttpRequest
* Content-Type: application/x-www-form-urlencoded' * 内容类型:application / x-www-form-urlencoded'



Why the Content-Type not change as 'application/json; 为什么Content-Type不会更改为'application / json; charset=UTF-8'? charset = UTF-8'?

This question is really old, but nowadays it's a lot easier to use the requests package: 这个问题确实很老,但是如今使用requests包要容易得多:

import requests
head = { 'Content-Type': 'application/json; charset=UTF-8',
         'Accept': 'application/json, text/javascript, */*; q=0.01',
         'X-Requested-With': 'XMLHttpRequest' }
data = dict(dispatchorderid="966A48E572624F2FB2E99F371C232729")
response = requests.post(url, json.dumps(data), headers=head)

print response.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM