1. 使用 requests发送GET/POST请求
import requests
#设置请求参数,以字典key-val形式存储
payload={
'foo1':'bar1','foo2':'bar2'}
#设置请求头信息,以字典key-val形式存储
header={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
'Cookie':'TestCookie=True',
'Accept-Encoding':'gzip, deflate'
}
#使用requests发送一个GET请求
r1=requests.get('http://postman-echo.com/get',params=payload,headers=header)
print('GET请求的响应结果是:',r1.text)
#使用requests发送一个POST请求
r2=requests.post('http://postman-echo.com/post',data=payload,headers=header)
print('POST请求的响应结果是:',r2.text)
运行结果是:
GET请求的响应结果是: {
"args":{
"foo1":"bar1","foo2":"bar2"},"headers":{
"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-5f8dab77-5978cb2c56f34e512597f2fa","user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36","accept-encoding":"gzip, deflate","accept":"*/*","cookie":"TestCookie=True"},"url":"http://postman-echo.com/get?foo1=bar1&foo2=bar2"}
POST请求的响应结果是: {
"args":{
},"data":"","files":{
},"form":{
"foo1":"bar1","foo2":"bar2"},"headers":{
"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-5f8dab78-3f18ac1f1dff63012c6a9905","content-length":"19","user-agent"