http://blog.csdn.net/php_fly/article/details/17171985

执行结果

代码

[cpp]view plain copy
  1. // getinfo.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. #include "curl/curl.h"
  7. #pragma comment(lib, "curllib.lib")
  8. //回调函数
  9. size_t process_data(void *buffer, size_t size, size_t nmemb, void *user_p)
  10. {
  11. FILE *fp = (FILE *)user_p;
  12. size_t return_size = fwrite(buffer, size, nmemb, fp);
  13. //cout << (char *)buffer << endl;
  14. return return_size;
  15. }
  16. void print_cookies(CURL *curl)
  17. {
  18. CURLcode res;
  19. struct curl_slist *cookies;
  20. struct curl_slist *nc;
  21. int i;
  22. printf("Cookies, curl knows:\n");
  23. res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  24. if (res != CURLE_OK) {
  25. fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
  26. exit(1);
  27. }
  28. nc = cookies, i = 1;
  29. while (nc) {
  30. printf("[%d]: %s\n", i, nc->data);
  31. nc = nc->next;
  32. i++;
  33. }
  34. if (i == 1) {
  35. printf("(none)\n");
  36. }
  37. curl_slist_free_all(cookies);
  38. }
  39. int _tmain(int argc, _TCHAR* argv[])
  40. {
  41. // 初始化libcurl
  42. CURLcode return_code;
  43. return_code = curl_global_init(CURL_GLOBAL_WIN32);
  44. if (CURLE_OK != return_code)
  45. {
  46. cerr << "init libcurl failed." << endl;
  47. return -1;
  48. }
  49. // 获取easy handle
  50. CURL *easy_handle = curl_easy_init();
  51. if (NULL == easy_handle)
  52. {
  53. cerr << "get a easy handle failed." << endl;
  54. curl_global_cleanup();
  55. return -1;
  56. }
  57. FILE *fp = fopen("data.html", "ab+");
  58. char *url = "http://blog.csdn.com/php_fly";
  59. //char *url = "http://www.csdn.com";
  60. // 设置easy handle属性
  61. curl_easy_setopt(easy_handle, CURLOPT_URL, url);
  62. curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
  63. // curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);
  64. //curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
  65. //fp:回调函数的参数
  66. curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
  67. // 执行数据请求
  68. return_code = curl_easy_perform(easy_handle);
  69. if (return_code != CURLE_OK)
  70. {
  71. printf( "Failed to get '%s' [%s]\n",url, return_code);
  72. return 0;
  73. }
  74. //
  75. int totalTime = 0;
  76. return_code = curl_easy_getinfo(easy_handle,CURLINFO_TOTAL_TIME,&totalTime);
  77. if((CURLE_OK==return_code) && totalTime)
  78. cout<<"耗时:"<<totalTime<<endl;
  79. long downLength = 0;
  80. return_code = curl_easy_getinfo(easy_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD,&downLength);
  81. if((CURLE_OK==return_code) && downLength)
  82. cout<<"下载的文件大小:"<<downLength<<endl;
  83. long retcode = 0;
  84. return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE , &retcode);
  85. if((CURLE_OK==return_code) && retcode)
  86. cout<<"状态码:"<<retcode<<endl;
  87. char *contentType={0};
  88. return_code = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_TYPE , &contentType);
  89. if((CURLE_OK==return_code) && contentType)
  90. cout<<"请求的文件类型:"<<contentType<<endl;
  91. //输出cookie信息
  92. print_cookies(easy_handle);
  93. long filetime = 0;
  94. return_code = curl_easy_getinfo(easy_handle, CURLINFO_FILETIME , &filetime);
  95. if((CURLE_OK==return_code) && filetime)
  96. cout<<"远程获取文档的时间:"<<filetime<<endl;
  97. long namelookuptime = 0;
  98. return_code = curl_easy_getinfo(easy_handle, CURLINFO_NAMELOOKUP_TIME , &namelookuptime);
  99. if((CURLE_OK==return_code) && namelookuptime)
  100. cout<<"名称解析所消耗的时间:"<<namelookuptime<<""<<endl;
  101. long requestSize = 0;
  102. return_code = curl_easy_getinfo(easy_handle, CURLINFO_REQUEST_SIZE , &requestSize);
  103. if((CURLE_OK==return_code) && requestSize)
  104. cout<<"请求头大小:"<<requestSize<<"字节"<<endl;
  105. long headerSize = 0;
  106. return_code = curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE , &headerSize);
  107. if((CURLE_OK==return_code) && headerSize)
  108. cout<<"响应头大小:"<<headerSize<<"字节"<<endl;
  109. //获取URL重定向地址
  110. curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, true);
  111. char* redirectUrl = {0};
  112. return_code = curl_easy_getinfo(easy_handle, CURLINFO_REDIRECT_URL , &redirectUrl);
  113. if((CURLE_OK==return_code) && redirectUrl)
  114. cout<<"URL重定向地址:"<<redirectUrl<<endl;
  115. else
  116. cout<<"URL重定向地址:"<<NULL<<endl;
  117. char *ipAddress={0};
  118. return_code = curl_easy_getinfo(easy_handle, CURLINFO_PRIMARY_IP,&ipAddress);
  119. if((CURLE_OK==return_code) && ipAddress)
  120. cout<<"请求的服务器IP:"<<ipAddress<<endl;
  121. long downloadSpeed = 0;
  122. return_code = curl_easy_getinfo(easy_handle, CURLINFO_SPEED_DOWNLOAD , &downloadSpeed);
  123. if((CURLE_OK==return_code) && downloadSpeed)
  124. printf("平均下载速度: %0.3f kb/s.\n", downloadSpeed/ 1024);
  125. //
  126. // 释放资源
  127. fclose(fp);
  128. curl_easy_cleanup(easy_handle);
  129. curl_global_cleanup();
  130. getchar();
  131. return 0;
  132. }

libcurl之curl_easy_getinfo的使用教程相关推荐

  1. linux libcurl 库使用方法

    一.ibcurl简介 作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, I ...

  2. libcurl API 常用函数

    libcurl 详参libcurl 1 curl_easy_getinfo CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... ); 使 ...

  3. http/https监控获取响应时间(DNS解析时间,RRT时间,服务器处理时间等)

    有时候为了测试网络情况,需要返回每个阶段的耗时时间,比如DNS解析耗时,建立连接所消耗的时间,从建立连接到准备传输所使用的时间,从建立连接到传输开始所使用的时间,整个过程耗时,下载的数据量,下载速度, ...

  4. C语言curl实现FTP上传、下载、获取文件信息

    目录 Get a single file from an FTP server. Checks a single file's size and mtime from an FTP server. G ...

  5. multi接口的使用

    这个文档是小编在curl官网上使用谷歌翻译翻译的,详细信息看官网 curl 描述 这是关于如何在 C 程序中使用 libcurl 多接口的概述.这里提到的每个函数都有特定的手册页.还有libcurl- ...

  6. 使用CURL检测Clinet侧发起的HTTP请求各阶段时间

    2019独角兽企业重金招聘Python工程师标准>>> 第一.HTTP请求的过程介绍 一个HTTP请求,涉及多个阶段 1.DNS解析域名 2.请求从Clinet路由至Server,C ...

  7. linux下c/c++方式访问curl的帮助手册

    转自:http://blog.chinaunix.net/u1/47395/showart_1768832.html 有个业务需求需要通过curl 代理的方式来访问外网 百度了一把,测试可以正常使用. ...

  8. libcurl 教程

    感谢原作者的辛勤劳作:https://blog.csdn.net/JGood/article/details/4787670 目标 本文档介绍了在应用程序开发过程中,如何正确使用libcurl的基本方 ...

  9. 【Curl (libcurl) 开发 之一】Cocos2dx之libcurl(curl_easy)的编程教程(帮助手册)!...

    本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/hibernate/783.html ☞ 点击 ...

  10. libcurl使用方法

    原文地址:http://curl.haxx.se/libcurl/c/libcurl-tutorial.html 译者:JGood(http://blog.csdn.net/JGood ) 译者注:这 ...

最新文章

  1. 基于 Webpack 3 的 React 工程项目脚手架
  2. python3 - 元组、集合
  3. c++11 常量表达式
  4. 单核工作法13:永不拖延(上)
  5. 如何连接Linux上的服务器 网络编程,Linux 网络编程 一
  6. 三款免费的PHP加速器:APC、eAccelerator、XCache比较
  7. CVPR 2019 | 步步为营!通过迭代式模糊核预测提高超分辨质量
  8. 2019年7月数据库流行度排行:Oracle王者归来获大幅增长
  9. 因未交赎金,世界航天巨头机密文档遭勒索软件公开
  10. google浏览器更新问题和路径问题
  11. Frodo and pillows _CF760B
  12. 有哪些好用的智能写作工具?
  13. 数据分析python面试题_10道Python常见面试题
  14. Cesium 实现粒子效果贴地(伪)
  15. 改变CEdit中字体大小与颜色
  16. oracle的空间数据库
  17. 空间注意力机制和通道注意力机制详解
  18. Python语言程序设计 - 测验6: 组合数据类型 (第6周)
  19. python实现自动点击器_Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)...
  20. android查询cpu信息

热门文章

  1. 腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
  2. 程序员的“三十而已”,你都30岁了,职业该如何规划?
  3. CommonAPI编写代码
  4. linux系统中 为mysql还原数据库_linux中mysql还原数据库命令
  5. LeetCode 75. Sort Colors(三颜色排序→K颜色排序)
  6. 向量积计算三角形面积
  7. 基础矩阵F和本质矩阵E
  8. ARX中各种坐标系及Transfrom操作相关
  9. 甘肃省庆阳市谷歌卫星地图下载
  10. 杏仁粉的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告