#C++#通过libcurl获取当前次HTTP请求的一些相关信息

2023-05-16

libcurl库是一个非常完整和好用的HTTP请求库,通过它,我们可以方便的书写HTTP请求程序,而且不需要关心HTTP协议内部的细节处理。

最近在做一个HTTP监测的程序,就是通过libcurl来实现的。通过查看官方文档发现,libcurl库几乎提供了所有我们需要的信息返回,如:域名解析时间,连接服务器时间,下载速度,上传速度,总响应时间,重定向次数,最后的有效URL等一系列和HTTP请求相关的信息,获取也非常方便,只要通过curl_easy_getinfo()函数加相关的参数就可以得到,详见:http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html


注:有的数据获取需要通过curl_easy_setopt ()配置了相关选项才可以得到。比如重定向就要配置了CURLOPT_FOLLOWLOCATION选项为1 

NAME

curl_easy_getinfo - extract information from a curl handle

SYNOPSIS

#include <curl/curl.h>

CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... );

DESCRIPTION

Request internal information from the curl session with this function. The third argument MUST be a pointer to a long, a pointer to a char *, a pointer to a struct curl_slist * or a pointer to a double (as this documentation describes further down). The data pointed-to will be filled in accordingly and can be relied upon only if the function returns CURLE_OK. Use this function AFTER a performed transfer if you want to get transfer- oriented data.

You should not free the memory returned by this function unless it is explicitly mentioned below.

AVAILABLE INFORMATION

The following information can be extracted:

CURLINFO_EFFECTIVE_URL

Pass a pointer to a char pointer to receive the last used effective URL.

CURLINFO_RESPONSE_CODE

Pass a pointer to a long to receive the last received HTTP or FTP code. This option was known as CURLINFO_HTTP_CODE in libcurl 7.10.7 and earlier. This will be zero if no server response code has been received. Note that a proxy's CONNECT response should be read with CURLINFO_HTTP_CONNECTCODE and not this.

CURLINFO_HTTP_CONNECTCODE

Pass a pointer to a long to receive the last received proxy response code to a CONNECT request.

CURLINFO_FILETIME

Pass a pointer to a long to receive the remote time of the retrieved document (in number of seconds since 1 jan 1970 in the GMT/UTC time zone). If you get -1, it can be because of many reasons (unknown, the server hides it or the server doesn't support the command that tells document time etc) and the time of the document is unknown. Note that you must tell the server to collect this information before the transfer is made, by using the CURLOPT_FILETIME option to curl_easy_setopt(3) or you will unconditionally get a -1 back. (Added in 7.5)

CURLINFO_TOTAL_TIME

Pass a pointer to a double to receive the total time in seconds for the previous transfer, including name resolving, TCP connect etc.

CURLINFO_NAMELOOKUP_TIME

Pass a pointer to a double to receive the time, in seconds, it took from the start until the name resolving was completed.

CURLINFO_CONNECT_TIME

Pass a pointer to a double to receive the time, in seconds, it took from the start until the connect to the remote host (or proxy) was completed.

CURLINFO_APPCONNECT_TIME

Pass a pointer to a double to receive the time, in seconds, it took from the start until the SSL/SSH connect/handshake to the remote host was completed. This time is most often very near to the PRETRANSFER time, except for cases such as HTTP pippelining where the pretransfer time can be delayed due to waits in line for the pipeline and more. (Added in 7.19.0)

CURLINFO_PRETRANSFER_TIME

Pass a pointer to a double to receive the time, in seconds, it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved. It does not involve the sending of the protocol- specific request that triggers a transfer.

CURLINFO_STARTTRANSFER_TIME

Pass a pointer to a double to receive the time, in seconds, it took from the start until the first byte is received by libcurl. This includes CURLINFO_PRETRANSFER_TIME and also the time the server needs to calculate the result.

CURLINFO_REDIRECT_TIME

Pass a pointer to a double to receive the total time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before final transaction was started. CURLINFO_REDIRECT_TIME contains the complete execution time for multiple redirections. (Added in 7.9.7)

CURLINFO_REDIRECT_COUNT

Pass a pointer to a long to receive the total number of redirections that were actually followed. (Added in 7.9.7)

CURLINFO_REDIRECT_URL

Pass a pointer to a char pointer to receive the URL a redirect would take you to if you would enable CURLOPT_FOLLOWLOCATION. This can come very handy if you think using the built-in libcurl redirect logic isn't good enough for you but you would still prefer to avoid implementing all the magic of figuring out the new URL. (Added in 7.18.2)

CURLINFO_SIZE_UPLOAD

Pass a pointer to a double to receive the total amount of bytes that were uploaded.

CURLINFO_SIZE_DOWNLOAD

Pass a pointer to a double to receive the total amount of bytes that were downloaded. The amount is only for the latest transfer and will be reset again for each new transfer.

CURLINFO_SPEED_DOWNLOAD

Pass a pointer to a double to receive the average download speed that curl measured for the complete download. Measured in bytes/second.

CURLINFO_SPEED_UPLOAD

Pass a pointer to a double to receive the average upload speed that curl measured for the complete upload. Measured in bytes/second.

CURLINFO_HEADER_SIZE

Pass a pointer to a long to receive the total size of all the headers received. Measured in number of bytes.

CURLINFO_REQUEST_SIZE

Pass a pointer to a long to receive the total size of the issued requests. This is so far only for HTTP requests. Note that this may be more than one request if FOLLOWLOCATION is true.

CURLINFO_SSL_VERIFYRESULT

Pass a pointer to a long to receive the result of the certification verification that was requested (using the CURLOPT_SSL_VERIFYPEER option to curl_easy_setopt(3)).

CURLINFO_SSL_ENGINES

Pass the address of a 'struct curl_slist *' to receive a linked-list of OpenSSL crypto-engines supported. Note that engines are normally implemented in separate dynamic libraries. Hence not all the returned engines may be available at run-time. NOTE: you must call curl_slist_free_all(3) on the list pointer once you're done with it, as libcurl will not free the data for you. (Added in 7.12.3)

CURLINFO_CONTENT_LENGTH_DOWNLOAD

Pass a pointer to a double to receive the content-length of the download. This is the value read from the Content-Length: field. Since 7.19.4, this returns -1 if the size isn't known.

CURLINFO_CONTENT_LENGTH_UPLOAD

Pass a pointer to a double to receive the specified size of the upload. Since 7.19.4, this returns -1 if the size isn't known.

CURLINFO_CONTENT_TYPE

Pass a pointer to a char pointer to receive the content-type of the downloaded object. This is the value read from the Content-Type: field. If you get NULL, it means that the server didn't send a valid Content-Type header or that the protocol used doesn't support this.

CURLINFO_PRIVATE

Pass a pointer to a char pointer to receive the pointer to the private data associated with the curl handle (set with the CURLOPT_PRIVATE option to curl_easy_setopt(3)). Please note that for internal reasons, the value is returned as a char pointer, although effectively being a 'void *'. (Added in 7.10.3)

CURLINFO_HTTPAUTH_AVAIL

Pass a pointer to a long to receive a bitmask indicating the authentication method(s) available. The meaning of the bits is explained in the CURLOPT_HTTPAUTH option forcurl_easy_setopt(3). (Added in 7.10.8)

CURLINFO_PROXYAUTH_AVAIL

Pass a pointer to a long to receive a bitmask indicating the authentication method(s) available for your proxy authentication. (Added in 7.10.8)

CURLINFO_OS_ERRNO

Pass a pointer to a long to receive the errno variable from a connect failure. Note that the value is only set on failure, it is not reset upon a successfull operation. (Added in 7.12.2)

CURLINFO_NUM_CONNECTS

Pass a pointer to a long to receive how many new connections libcurl had to create to achieve the previous transfer (only the successful connects are counted). Combined withCURLINFO_REDIRECT_COUNT you are able to know how many times libcurl successfully reused existing connection(s) or not. See the Connection Options ofcurl_easy_setopt(3) to see how libcurl tries to make persistent connections to save time. (Added in 7.12.3)

CURLINFO_PRIMARY_IP

Pass a pointer to a char pointer to receive the pointer to a zero-terminated string holding the IP address of the most recent connection done with this curl handle. This string may be IPv6 if that's enabled. Note that you get a pointer to a memory area that will be re-used at next request so you need to copy the string if you want to keep the information. (Added in 7.19.0)

CURLINFO_PRIMARY_PORT

Pass a pointer to a long to receive the destination port of the most recent connection done with this curl handle. (Added in 7.21.0)

CURLINFO_LOCAL_IP

Pass a pointer to a char pointer to receive the pointer to a zero-terminated string holding the local (source) IP address of the most recent connection done with this curl handle. This string may be IPv6 if that's enabled. The same restrictions apply as to CURLINFO_PRIMARY_IP. (Added in 7.21.0)

CURLINFO_LOCAL_PORT

Pass a pointer to a long to receive the local (source) port of the most recent connection done with this curl handle. (Added in 7.21.0)

CURLINFO_COOKIELIST

Pass a pointer to a 'struct curl_slist *' to receive a linked-list of all cookies cURL knows (expired ones, too). Don't forget to curl_slist_free_all(3) the list after it has been used. If there are no cookies (cookies for the handle have not been enabled or simply none have been received) 'struct curl_slist *' will be set to point to NULL. (Added in 7.14.1)

CURLINFO_LASTSOCKET

Pass a pointer to a long to receive the last socket used by this curl session. If the socket is no longer valid, -1 is returned. When you finish working with the socket, you must call curl_easy_cleanup() as usual and let libcurl close the socket and cleanup other resources associated with the handle. This is typically used in combination withCURLOPT_CONNECT_ONLY. (Added in 7.15.2)

NOTE: this API is not really working on win64, since the SOCKET type on win64 is 64 bit large while its 'long' is only 32 bits.

CURLINFO_FTP_ENTRY_PATH

Pass a pointer to a char pointer to receive a pointer to a string holding the path of the entry path. That is the initial path libcurl ended up in when logging on to the remote FTP server. This stores a NULL as pointer if something is wrong. (Added in 7.15.4)

Also works for SFTP since 7.21.4

CURLINFO_CERTINFO

Pass a pointer to a 'struct curl_certinfo *' and you'll get it set to point to struct that holds a number of linked lists with info about the certificate chain, assuming you had CURLOPT_CERTINFO enabled when the previous request was done. The struct reports how many certs it found and then you can extract info for each of those certs by following the linked lists. The info chain is provided in a series of data in the format "name:content" where the content is for the specific named data. See also the certinfo.c example. NOTE: this option is only available in libcurl built with OpenSSL support. (Added in 7.19.1)

CURLINFO_CONDITION_UNMET

Pass a pointer to a long to receive the number 1 if the condition provided in the previous request didn't match (see CURLOPT_TIMECONDITION). Alas, if this returns a 1 you know that the reason you didn't get data in return is because it didn't fulfill the condition. The long ths argument points to will get a zero stored if the condition instead was met. (Added in 7.19.4)

CURLINFO_RTSP_SESSION_ID

Pass a pointer to a char pointer to receive a pointer to a string holding the most recent RTSP Session ID.

Applications wishing to resume an RTSP session on another connection should retreive this info before closing the active connection.

CURLINFO_RTSP_CLIENT_CSEQ

Pass a pointer to a long to receive the next CSeq that will be used by the application.

CURLINFO_RTSP_SERVER_CSEQ

Pass a pointer to a long to receive the next server CSeq that will be expected by the application.

(NOTE: listening for server initiated requests is currently unimplemented).

Applications wishing to resume an RTSP session on another connection should retreive this info before closing the active connection.

CURLINFO_RTSP_CSEQ_RECV

Pass a pointer to a long to receive the most recently received CSeq from the server. If your application encounters a CURLE_RTSP_CSEQ_ERROR then you may wish to troubleshoot and/or fix the CSeq mismatch by peeking at this value.

TIMES

An overview of the six time values available from curl_easy_getinfo()

curl_easy_perform()   |   |--NAMELOOKUP   |--|--CONNECT   |--|--|--APPCONNECT   |--|--|--|--PRETRANSFER   |--|--|--|--|--STARTTRANSFER   |--|--|--|--|--|--TOTAL   |--|--|--|--|--|--REDIRECT

NAMELOOKUP

CURLINFO_NAMELOOKUP_TIME. The time it took from the start until the name resolving was completed.

CONNECT

CURLINFO_CONNECT_TIME. The time it took from the start until the connect to the remote host (or proxy) was completed.

APPCONNECT

CURLINFO_APPCONNECT_TIME. The time it took from the start until the SSL connect/handshake with the remote host was completed. (Added in in 7.19.0)

PRETRANSFER

CURLINFO_PRETRANSFER_TIME. The time it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.

STARTTRANSFER

CURLINFO_STARTTRANSFER_TIME. The time it took from the start until the first byte is received by libcurl.

TOTAL

CURLINFO_TOTAL_TIME. Total time of the previous request.

REDIRECT

CURLINFO_REDIRECT_TIME. The time it took for all redirection steps include name lookup, connect, pretransfer and transfer before final transaction was started. So, this is zero if no redirection took place.

RETURN VALUE

If the operation was successful, CURLE_OK is returned. Otherwise an appropriate error code will be returned.


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

#C++#通过libcurl获取当前次HTTP请求的一些相关信息 的相关文章

随机推荐

  • 大量数据情况下单线程插入和多线程insert数据库的性能测试

    大量数据情况下单线程插入和多线程insert数据库的性能测试 之前一直没有遇到过大批量数据入库的场景 xff0c 所以一直没有思考过在大量数据的情况下单线程插入和多线程插入的性能情况 今天在看一个项目源代码的时候发现使用了多线程insert
  • 查看tensorflow 安装目录

    使用命令 xff1a pip show f tensorflow 图和张量源码 xff1a C Program Files Anaconda3 Lib site packages tensorflow python framework op
  • FP-growth算法,fpgrowth算法详解

    FP growth算法 xff0c fpgrowth算法详解 使用FP growth算法来高效发现频繁项集 前言 你用过搜索引擎挥发现这样一个功能 xff1a 输入一个单词或者单词的一部分 xff0c 搜索引擎酒会自动补全查询词项 xff0
  • 如何将一个矩阵化为行阶梯形矩阵

    2016 03 29 尾巴 线性代数 有同学反映上一课过于冷冰冰 xff0c 都是一些不带证明的公式 如果线性代数所有公式都要证明的话 xff0c 线性代数的难度会上好几个量级 xff0c 有的公式的证明是特别特别难的 还有一个 xff0c
  • Activity启动模式与任务栈(Task)全面深入记录(上)

    转载请注明出处 xff08 谢谢 xff09 xff1a http blog csdn net javazejian article details 52071885 任务栈简单入门 最近又把两本进阶书看了一遍 xff0c 但总感觉好记性不
  • VSCode自定义代码片段3——url大全

    url大全 url 39 3 如何自定义用户代码片段 xff1a VSCode 61 左下角设置 61 用户代码片段 61 新建全局代码片段文件 61 自定义片段名称 61 编辑用户片段 61 ctrl 43 S 保存 url大全 34 P
  • mac中如何使用vsode愉快地运行C、C++程序

    闲来无事 xff0c 想在mac中写写C C 43 43 程序 xff0c 打开应用商店 xff0c 准备下个xcode玩玩 xff0c 结果小30G的空间占用直接劝退 因为一直使用vscode开发 xff0c 便萌生了使用插件来运行C C
  • CMake入门3之 静态链接库和动态链接库

    目录 目标 xff1a 环境 创建静态和动态链接库 文件准备 构建 安装静态链接库 使用静态链接库和动态库 使用动态库 使用静态库 目标 xff1a 构建静态链接库和动态链接库 xff0c 安装到系统 xff0c 写一个程序使用安装的静态链
  • 行人检测数据集

    MIT数据库 该数据库为较早公开的行人数据库 xff0c 共924张行人图片 xff08 ppm格式 xff0c 宽高为64 128 xff09 xff0c 肩到脚的距离约80象素 该数据库只含正面和背面两个视角 xff0c 无负样本 xf
  • opencv中solvePnPRansac函数求解相机位姿

    PnP xff08 Perspective n Points xff09 xff1a 2D 3D xff0c 求解相机位姿 PnP xff08 Perspective n Points xff09 就是你有n个点的3D位置和它们的投影 xf
  • 如何查看ubuntu下显卡驱动是否已经成功安装

    首先得安装 mesa utils xff0c 在终端输入命令 xff1a sudo apt get install mesa utils 然后再运行命令 xff1a glxinfo grep rendering 如果结果是 yes xff0
  • 深度学习笔记1:end-to-end、anchor box解释、人体检测代码

    SSD xff08 SSD Single Shot MultiBox Detector xff09 是采用单个深度神经网络模型实现目标检测和识别的方法 该方法是综合了Faster R CNN的anchor box和YOLO单个神经网络检测思
  • YOLO-darknet-on-Jetson-TX2

    YOLO darknet on Jetson TX2 https github com Alro10 YOLO darknet on Jetson TX2 software https blog csdn net cherry dr art
  • #error This file requires compiler and library support for the ISO C++ 2011错误解决办法

    CmakeLists txt 中添如下代码就OK include CheckCXXCompilerFlag CHECK CXX COMPILER FLAG 34 std 61 c 43 43 11 34 COMPILER SUPPORTS
  • docker pull 下来的镜像文件存放的位置

    参考文献 xff1a http www tuicool com articles bENrUf 1 存放在 var lib docker 2 repositories aufs 文件 xff1a 34 Repositories 34 34
  • docker主要命令整理

    docker docker info docker version docker images 展示镜像 docker ps 展示容器 docker ps a 会展示出所有正在运行的和已经停止的容器 docker rmi 镜像ID 删除镜像
  • python xml读写

    1 xml例子 lt xml version 61 34 1 0 34 encoding 61 34 UTF 8 34 gt lt annotation gt lt folder gt VOC2007 lt folder gt lt fil
  • caffe2编译问题:avx、avx2

    caffe2编译问题 xff1a 9898 init intrinsics check cc 54 CPU feature avx is present on your machine but the Caffe2 binary is no
  • 【Hello,电赛】2020电赛从备战到回顾(F题)

    瞎扯几句 2020应该让所有人难忘的一年 赋闲大半年 xff0c 开学两行泪 博主也是突然从大二被迫成了大三老油条 大一的时候身边就有同学开始展露科创竞赛方面的头角 xff0c 后来成了学院有名的带佬 咱当时觉得好玩也是羡慕 xff0c 奈
  • #C++#通过libcurl获取当前次HTTP请求的一些相关信息

    libcurl库是一个非常完整和好用的HTTP请求库 xff0c 通过它 xff0c 我们可以方便的书写HTTP请求程序 xff0c 而且不需要关心HTTP协议内部的细节处理 最近在做一个HTTP监测的程序 xff0c 就是通过libcur