本文介绍了如何使用cURL或python请求将参数编码为gbk而不是utf-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个奇怪的API,它只接受 gbk
参数,我在 Windows中捕获数据
IE浏览器
,使用此命令显示数据:
I have a strange API, it just accept gbk
parameters, I capture the data in Windows
IE browser
, show data with this command:
$ cat 12_Request.txt| iconv -f GBK -t UTF-8
GET http://10.202.15.197:20176/?user_id=1&query_type=GEOSPLIT&address=广东省深圳市宝安&ret_splitinfo=1 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: 10.202.15.197:20176
DNT: 1
Connection: Keep-Alive
如您所见,我的数据编码为 GBK
即可。
然后我用 netcat
发送数据,如下所示:
As you can see, my data is encode in GBK
.Then I send data with netcat
like this:
$ cat 12_Request.txt| nc 10.202.15.197 20176 | iconv -f GBK -t utf-8 # right
HTTP/1.0 200 OK
Content-Type: application/octet-stream
Connection: close
Content-Length: 222
<?xml version='1.0' encoding='GBK'?>
<addrSplitInfo>
<status>0</status><as_info prop="1" level="1">广东省</as_info>
<as_info prop="1" level="2">深圳市</as_info>
<as_info prop="3" level="18">宝安</as_info>
</addrSplitInfo>
但如果我使用 UTF-8发送数据
,我的回复错误:
but if I send data with UTF-8
, I get the wrong response:
$ cat 12_Request.txt | iconv -f GBK -t utf-8 | nc 10.202.15.197 20176 # wrong
HTTP/1.0 200 OK
Content-Type: application/octet-stream
Connection: close
Content-Length: 152
<?xml version='1.0' encoding='GBK'?>
<addrSplitInfo>
<status>0</status><as_info prop="6" level="13">广东省深圳市宝安</as_info>
</addrSplitInfo>
我试图在 cURL
:
curl -v \
--header 'Accept: text/html, application/xhtml+xml, */*' \
--header 'Accept-Language: zh-CN' \
-A 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko' \
--header 'Accept-Encoding: gzip, deflate' \
--header 'Host: 10.202.15.197:20176' \
--header 'DNT: 1' \
--header 'Connection: Keep-Alive' \
http://10.202.15.197:20176?user_id=1&query_type=GEOSPLIT&address=广东省深圳市宝安&ret_splitinfo=1
这不起作用,它得到回复如下所示:
this doesn't work, it get response as follow:
<?xml version='1.0' encoding='GBK'?>
<addrSplitInfo>
<status>0</status><as_info prop="6" level="13">广东省深圳市宝安</as_info>
</addrSplitInfo>
然后,使用 data-urlencode
:
curl \
--header 'Accept: text/html, application/xhtml+xml, */*' \
--header 'Accept-Language: zh-CN' \
-A 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko' \
--header 'Accept-Encoding: gzip, deflate' \
--header 'Host: 10.202.15.197:20176' \
--header 'DNT: 1' \
--header 'Connection: Keep-Alive' \
http://10.202.15.197:20176 --data-urlencode 'user_id=1&query_type=GEOSPLIT&address=广东省深圳市宝安&ret_splitinfo=1'
这不起作用,它获取响应如下:
<?xml version='1.0' encoding='GBK'?>
<searchresult><status>1</status><count>0</count>
</searchresult>
我也试过python,
Also I tried with python,
>>> import requests
>>> url = u"http://10.202.15.197:20176?user_id=1&query_type=GEOSPLIT&address=广东省深圳市宝安&ret_splitinfo=1"
>>> r = requests.get(url.encode('utf-8').decode('gbk'))
>>> print r.text
这不起作用,响应如下:
u'<?xml version=\'1.0\' encoding=\'GBK\'?>\n<addrSplitInfo>\n<status>0</status><as_info prop="6" level="13">\xe9\u015e\x9e\u013a\xb8\xe7\u0179\u02d8\xe9\x90\u015e\xe4\u02dd\u0161\xe7\u0161\x81\xe9\x8d)\x86\u02db\xe7\u0164\u015b\xe7\x80\u0161\u0107\u017c\x86\xe7\x95\xa8</as_info>\n</addrSplitInfo>\n'
推荐答案
我想我得到了答案
cURL
:
echo "http://10.202.15.197:20176\?user_id\=1\&query_type\=GEOSPLIT\&address\=广东省深圳市宝安\&ret_splitinfo\=1" | iconv -f utf-8 -t gbk | xargs curl
with python
with python
payload = {"user_id": 1, "query_type": "GEOSPLIT", "address": u"广东省深圳市宝安".encode('gbk'), "ret_splitinfo": 1}
r = requests.get("http://10.202.15.197:20176", payload)
print r.text
这篇关于如何使用cURL或python请求将参数编码为gbk而不是utf-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!