libcurl之curl_easy_getinfo的使用教程
http://blog.csdn.net/php_fly/article/details/17171985
执行结果
代码
- // getinfo.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #include "curl/curl.h"
- #pragma comment(lib, "curllib.lib")
- //回调函数
- size_t process_data(void *buffer, size_t size, size_t nmemb, void *user_p)
- {
- FILE *fp = (FILE *)user_p;
- size_t return_size = fwrite(buffer, size, nmemb, fp);
- //cout << (char *)buffer << endl;
- return return_size;
- }
- void print_cookies(CURL *curl)
- {
- CURLcode res;
- struct curl_slist *cookies;
- struct curl_slist *nc;
- int i;
- printf("Cookies, curl knows:\n");
- res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
- if (res != CURLE_OK) {
- fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
- exit(1);
- }
- nc = cookies, i = 1;
- while (nc) {
- printf("[%d]: %s\n", i, nc->data);
- nc = nc->next;
- i++;
- }
- if (i == 1) {
- printf("(none)\n");
- }
- curl_slist_free_all(cookies);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- // 初始化libcurl
- CURLcode return_code;
- return_code = curl_global_init(CURL_GLOBAL_WIN32);
- if (CURLE_OK != return_code)
- {
- cerr << "init libcurl failed." << endl;
- return -1;
- }
- // 获取easy handle
- CURL *easy_handle = curl_easy_init();
- if (NULL == easy_handle)
- {
- cerr << "get a easy handle failed." << endl;
- curl_global_cleanup();
- return -1;
- }
- FILE *fp = fopen("data.html", "ab+");
- char *url = "http://blog.csdn.com/php_fly";
- //char *url = "http://www.csdn.com";
- // 设置easy handle属性
- curl_easy_setopt(easy_handle, CURLOPT_URL, url);
- curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
- // curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);
- //curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
- //fp:回调函数的参数
- curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
- // 执行数据请求
- return_code = curl_easy_perform(easy_handle);
- if (return_code != CURLE_OK)
- {
- printf( "Failed to get '%s' [%s]\n",url, return_code);
- return 0;
- }
- //
- int totalTime = 0;
- return_code = curl_easy_getinfo(easy_handle,CURLINFO_TOTAL_TIME,&totalTime);
- if((CURLE_OK==return_code) && totalTime)
- cout<<"耗时:"<<totalTime<<endl;
- long downLength = 0;
- return_code = curl_easy_getinfo(easy_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD,&downLength);
- if((CURLE_OK==return_code) && downLength)
- cout<<"下载的文件大小:"<<downLength<<endl;
- long retcode = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE , &retcode);
- if((CURLE_OK==return_code) && retcode)
- cout<<"状态码:"<<retcode<<endl;
- char *contentType={0};
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_TYPE , &contentType);
- if((CURLE_OK==return_code) && contentType)
- cout<<"请求的文件类型:"<<contentType<<endl;
- //输出cookie信息
- print_cookies(easy_handle);
- long filetime = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_FILETIME , &filetime);
- if((CURLE_OK==return_code) && filetime)
- cout<<"远程获取文档的时间:"<<filetime<<endl;
- long namelookuptime = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_NAMELOOKUP_TIME , &namelookuptime);
- if((CURLE_OK==return_code) && namelookuptime)
- cout<<"名称解析所消耗的时间:"<<namelookuptime<<""<<endl;
- long requestSize = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_REQUEST_SIZE , &requestSize);
- if((CURLE_OK==return_code) && requestSize)
- cout<<"请求头大小:"<<requestSize<<"字节"<<endl;
- long headerSize = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE , &headerSize);
- if((CURLE_OK==return_code) && headerSize)
- cout<<"响应头大小:"<<headerSize<<"字节"<<endl;
- //获取URL重定向地址
- curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, true);
- char* redirectUrl = {0};
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_REDIRECT_URL , &redirectUrl);
- if((CURLE_OK==return_code) && redirectUrl)
- cout<<"URL重定向地址:"<<redirectUrl<<endl;
- else
- cout<<"URL重定向地址:"<<NULL<<endl;
- char *ipAddress={0};
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_PRIMARY_IP,&ipAddress);
- if((CURLE_OK==return_code) && ipAddress)
- cout<<"请求的服务器IP:"<<ipAddress<<endl;
- long downloadSpeed = 0;
- return_code = curl_easy_getinfo(easy_handle, CURLINFO_SPEED_DOWNLOAD , &downloadSpeed);
- if((CURLE_OK==return_code) && downloadSpeed)
- printf("平均下载速度: %0.3f kb/s.\n", downloadSpeed/ 1024);
- //
- // 释放资源
- fclose(fp);
- curl_easy_cleanup(easy_handle);
- curl_global_cleanup();
- getchar();
- return 0;
- }
libcurl之curl_easy_getinfo的使用教程相关推荐
- linux libcurl 库使用方法
一.ibcurl简介 作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, I ...
- libcurl API 常用函数
libcurl 详参libcurl 1 curl_easy_getinfo CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... ); 使 ...
- http/https监控获取响应时间(DNS解析时间,RRT时间,服务器处理时间等)
有时候为了测试网络情况,需要返回每个阶段的耗时时间,比如DNS解析耗时,建立连接所消耗的时间,从建立连接到准备传输所使用的时间,从建立连接到传输开始所使用的时间,整个过程耗时,下载的数据量,下载速度, ...
- C语言curl实现FTP上传、下载、获取文件信息
目录 Get a single file from an FTP server. Checks a single file's size and mtime from an FTP server. G ...
- multi接口的使用
这个文档是小编在curl官网上使用谷歌翻译翻译的,详细信息看官网 curl 描述 这是关于如何在 C 程序中使用 libcurl 多接口的概述.这里提到的每个函数都有特定的手册页.还有libcurl- ...
- 使用CURL检测Clinet侧发起的HTTP请求各阶段时间
2019独角兽企业重金招聘Python工程师标准>>> 第一.HTTP请求的过程介绍 一个HTTP请求,涉及多个阶段 1.DNS解析域名 2.请求从Clinet路由至Server,C ...
- linux下c/c++方式访问curl的帮助手册
转自:http://blog.chinaunix.net/u1/47395/showart_1768832.html 有个业务需求需要通过curl 代理的方式来访问外网 百度了一把,测试可以正常使用. ...
- libcurl 教程
感谢原作者的辛勤劳作:https://blog.csdn.net/JGood/article/details/4787670 目标 本文档介绍了在应用程序开发过程中,如何正确使用libcurl的基本方 ...
- 【Curl (libcurl) 开发 之一】Cocos2dx之libcurl(curl_easy)的编程教程(帮助手册)!...
本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/hibernate/783.html ☞ 点击 ...
- libcurl使用方法
原文地址:http://curl.haxx.se/libcurl/c/libcurl-tutorial.html 译者:JGood(http://blog.csdn.net/JGood ) 译者注:这 ...
最新文章
- 基于 Webpack 3 的 React 工程项目脚手架
- python3 - 元组、集合
- c++11 常量表达式
- 单核工作法13:永不拖延(上)
- 如何连接Linux上的服务器 网络编程,Linux 网络编程 一
- 三款免费的PHP加速器:APC、eAccelerator、XCache比较
- CVPR 2019 | 步步为营!通过迭代式模糊核预测提高超分辨质量
- 2019年7月数据库流行度排行:Oracle王者归来获大幅增长
- 因未交赎金,世界航天巨头机密文档遭勒索软件公开
- google浏览器更新问题和路径问题
- Frodo and pillows _CF760B
- 有哪些好用的智能写作工具?
- 数据分析python面试题_10道Python常见面试题
- Cesium 实现粒子效果贴地(伪)
- 改变CEdit中字体大小与颜色
- oracle的空间数据库
- 空间注意力机制和通道注意力机制详解
- Python语言程序设计 - 测验6: 组合数据类型 (第6周)
- python实现自动点击器_Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)...
- android查询cpu信息
热门文章
- 腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
- 程序员的“三十而已”,你都30岁了,职业该如何规划?
- CommonAPI编写代码
- linux系统中 为mysql还原数据库_linux中mysql还原数据库命令
- LeetCode 75. Sort Colors(三颜色排序→K颜色排序)
- 向量积计算三角形面积
- 基础矩阵F和本质矩阵E
- ARX中各种坐标系及Transfrom操作相关
- 甘肃省庆阳市谷歌卫星地图下载
- 杏仁粉的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告