python 发送http请求填坑
2017-06-06 20:23
134 查看
先说问题:最近开始使用python的urllib2和urllib来请求webservice获取数据,但是服务器是java的服务器,发现python传入的参数(中文)在服务器端显示的是乱码,在网上搜索了一整天,终于整出来了,特此填坑。
1.环境:python2.7.10,rhel5.52.代码:
import urllib2 import urllib url = "https://www.baidu.com" para1 = {'haha',"哈哈"} #因为有中文,所以用 urllib.quote() para1 = urllib.quote(para1) para = urllib.urlencode(para1) f = urllib2.urlopen(url,data=para) buf = f.read() f.close()
主要函数功能说明:
urllib.quote(string[, safe]):对字符串进行编码。参数 safe 指定了不需要编码的字符。
urllib.unquote(string) :对字符串进行解码。
urllib.urlencode(query[, doseq]):将dict或者包含两个元素的元组列表转换成url参数。
注意:java服务器端需要修改部分代码,需要将请求参数进行utf-8解码。
相关文章推荐
- Python中使用socket发送HTTP请求数据接收不完整问题解决方法
- 转载:python发送HTTP请求
- python socket向百度发送http长连接请求 并做搜索
- [原创] Python 使用指定的网卡发送HTTP请求
- Python发送http请求解析返回json的实例
- Python发送HTTP GET请求
- python发送http请求之requests模块
- python通过http请求发送soap报文进行webservice接口调用
- 【转】 python socket向百度发送http长连接请求 并做搜索
- Python中使用socket发送HTTP请求数据接收不完整问题解决方法
- 使用Python发送http post请求上传文件
- python发送http请求之requests模块
- python 发送http请求
- python socket向百度发送http长连接请求 并做搜索
- python发送http请求之requests模块
- python socket向百度发送http长连接请求 并做搜索
- python socket向百度发送http长连接请求 并做搜索
- 利用python的socket发送http(s)请求方法示例
- 【转】 python socket向百度发送http长连接请求 并做搜索
- Python发送http请求解析返回json小demo