python - 同样的参数使用requests库得不到返回结果,而httplib可以正确返回。可能的原因是什么?
迷茫
迷茫 2017-04-17 16:42:06
[Python讨论组]
# -*- 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()
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(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)"}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号