HTTP协议简介和Request使用
一、HTTP协议
“超文本传输协议”
1. Request
协议头(header)
key | value |
---|---|
Host | 表示服务器主机的地址和端口 |
Content-Length | 表示body的数据长度,字节 |
Content-Type | 表示body的数据格式 |
User-Agent | 表示浏览器或操作系统的属性 |
Referer | 比哦啊是这个界面是从哪个界面跳转过来的 |
Cookie | 浏览器提供的让程序员在本地存储数据的能力 |
Content-Type参数:
application/x-www-form-urlencoded
multipart/form-data
application/json
2. Response
协议头(header)
基本格式与请求报头基本一致,其中Content-Type参数如下:
text/html
text/css
application/javascript
application/json
二、 HTTP请求
下面以requests为例
requests.get(url)
当手工构建url时,requests库允许使用param参数智能构建url,如:
url = "http://httpbin.org/get"
informations = {'key1': 'value1', 'key2': 'value2'} # Dict
r = requests.get(url, params=informations)
>>>print(r.url)
http://httpbin.org/get?key2=value2&key1=value1
post请求中传递参数的方式(data和json)的区别
- data:Content-Type 默认为
application/x-www-form-urlencoded
- json:Content-Type 默认为
application/json
(未完。。。待续)