现在的位置: 首页 > 综合 > 正文

libcurl之curl_easy_getinfo的使用教程

2013年12月18日 ⁄ 综合 ⁄ 共 3921字 ⁄ 字号 评论关闭

执行结果

代码

// 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;
}

版权

原文地址:曾是土木人

【上篇】
【下篇】

抱歉!评论已关闭.