http发送get请求
时间: 2023-08-03 22:06:20 浏览: 58
要发送 HTTP GET 请求,可以使用 Python 的 requests 库。以下是一个简单的示例代码:
```python
import requests
url = 'http://example.com/api/data'
response = requests.get(url)
if response.status_code == 200:
# 成功获取数据
data = response.json()
print(data)
else:
# 获取数据失败
print('请求失败,状态码为', response.status_code)
```
在这个例子中,我们使用 requests 库发送了一个 HTTP GET 请求到 http://example.com/api/data 这个 URL,然后获取了返回的 JSON 数据。如果请求成功,将会打印出这些数据;否则,将会输出错误信息。
相关问题
resty.http发送get请求
在 OpenResty 中,可以使用 `resty.http` 模块来发送 HTTP 请求。以下是一个发送 GET 请求的示例代码:
```lua
local http = require("resty.http")
local httpc = http.new()
local params = {
method = "GET",
ssl_verify = false -- 如果请求的是 HTTPS 站点,需要设置为 false
}
local res, err = httpc:request_uri("https://www.example.com", params)
if not res then
ngx.log(ngx.ERR, "failed to request: ", err)
return
end
ngx.say(res.body)
```
上述代码中,使用 `http.new()` 创建一个 `httpc` 对象,然后设置请求参数,最后使用 `request_uri` 方法发送请求。如果请求成功,可以通过 `res.body` 获取响应内容。
如果需要发送带参数的 GET 请求,可以将参数拼接到 URL 后面,例如:
```lua
local http = require("resty.http")
local httpc = http.new()
local params = {
method = "GET",
ssl_verify = false -- 如果请求的是 HTTPS 站点,需要设置为 false
}
local url = "https://www.example.com?param1=value1¶m2=value2"
local res, err = httpc:request_uri(url, params)
if not res then
ngx.log(ngx.ERR, "failed to request: ", err)
return
end
ngx.say(res.body)
```
注意,以上代码中,如果请求的是 HTTPS 站点,需要设置 `ssl_verify` 为 `false`,否则会报 SSL 证书验证错误。建议在生产环境中使用有效的 SSL 证书,并开启证书验证。
发送http get请求
好的,您可以使用以下 Python 代码发送 HTTP GET 请求:
```python
import requests
response = requests.get('http://example.com')
print(response.content)
```
上述代码中,我们使用 requests 库发送 HTTP GET 请求,URL 参数为 http://example.com。请求发送后,我们打印出响应的内容(response.content)。您可以根据自己的需要修改 URL 参数和其他请求参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)