libcurl之curl_easy_getinfo的使用教程

执行结果


代码


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

版权

原文地址: 曾是土木人
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值