同样的参数使用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)"}
                
#  使用requests库,返回结果错误                
res = requests.post(url,data=jsonstr,headers = headers)
print res.text

# 使用httplib 返回结果正常


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()
阅读 4.9k
1 个回答
新手上路,请多包涵
headers = {"Content-type":"application/json","Accept":"application/json",
"Accept-Encoding": "identity",
                "Connection":"Keep-Alive",
                "User-Agent": "MusicSample/2.4.2 (iPhone; iOS 9.2; Scale/3.00)"}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题