HttpRequest发送Post,Get请求例子(包含设置请求头信息和获取返回头信息
时间: 2024-02-01 16:12:40 浏览: 172
发送POST请求的例子(包含设置请求头信息和获取返回头信息):
```python
import requests
url = "http://www.example.com/api/test"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Content-Type': 'application/json'
}
data = {
"param1": "value1",
"param2": "value2"
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.headers)
print(response.json())
```
发送GET请求的例子(包含设置请求头信息和获取返回头信息):
```python
import requests
url = "http://www.example.com/api/test?param1=value1¶m2=value2"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
response = requests.get(url, headers=headers)
print(response.status_code)
print(response.headers)
print(response.json())
```
注意,以上代码中的url、headers、data等参数需要根据实际情况进行修改。另外,需要在代码前面导入requests库。
阅读全文