怎么看libcurl发出请求的http返回
时间: 2023-09-16 22:12:58 浏览: 74
要查看libcurl发出请求的HTTP响应,可以使用`curl_easy_setopt`函数设置`CURLOPT_VERBOSE`选项并将其值设置为`1`。这样,libcurl将会输出详细的调试信息,包括请求和响应的头部和正文。
例如,下面的代码设置了`CURLOPT_VERBOSE`选项并将其值设置为`1`,并使用libcurl发送一个HTTP GET请求:
```c
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // 开启调试输出
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
```
运行这个程序,您将会在终端中看到类似下面的输出:
```
* Trying 93.184.216.34:80...
* TCP_NODELAY set
* Connected to example.com (93.184.216.34) port 80 (#0)
> GET / HTTP/1.1
Host: example.com
User-Agent: curl/7.68.0
Accept: */*
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Mon, 09 Aug 2021 13:33:51 GMT
< Etag: "3147526947+ident"
< Expires: Mon, 16 Aug 2021 13:33:51 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (ewr/15BD)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
<
<!doctype html>
<html>
<head>
...
```
其中包含了请求和响应的头部和正文。
阅读全文