同样的参数使用requests库得不到返回结果,而httplib可以正确返回。可能的原因是什么?
# -*- coding: utf-8 -*-
import requests
import json
url = 'http://www.demodemo.cc/rpc/IUserService'
params = {
"jsonrpc": "1.1",
"method": "getAllMyRecommend_v2_3",
"id": {"sid": "jgjycleypyvciibjupanfmbpplyibvsr", "sign": "d41d8cd98f00b204e9800998ecf8427e", "uid": 17139,
"cVer": "21"},
"params": [17139, 0, 10]
}
jsonstr = json.dumps(params)
headers = {"Content-type": "application/json", "Accept": "application/json",
"Connection": "Keep-Alive",
"User-Agent": "MusicSample/2.4.2 (iPhone; iOS 9.2; Scale/3.00)"}
# use httplib can not get correct response
res = requests.post(url, data=jsonstr, headers=headers)
print res.text
# use httplib can get correct response
import httplib
httpClient = None
try:
httpClient = httplib.HTTPConnection("www.demodemo.cc", 80, timeout=30)
httpClient.request(method="POST", url="/rpc/IUserService", body=jsonstr, headers=headers)
response = httpClient.getresponse()
print response.status
print response.reason
print response.read()
print response.getheaders() # huoqu header
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()