利用libcurl下载图片

本文详细介绍了如何利用libcurl库在程序中实现图片的下载功能,从设置URL到保存文件,步骤清晰,实践效果满意。
摘要由CSDN通过智能技术生成
//Crawl.cpp

#include "Crawl.h"


using namespace std;
 
CCrawl::CCrawl()
{
}

CCrawl::~CCrawl()
{
}

size_t  CCrawl::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{

	size_t realsize = size * nmemb;

	struct MemoryStruct *mem = (struct MemoryStruct *)userp;

	char *ptr = (char *)realloc(mem->memory, mem->size + realsize + 1);

	if(ptr == NULL) {
		    /* out of memory! */ 
		printf("not enough memory (realloc returned NULL)\n");
	//	exit(1);
		return 0;
	}

	mem->memory = ptr;
	memcpy(&(mem->memory[mem->size]), contents, realsize);
	mem->size += realsize;
	mem->memory[mem->size] = 0;

	return realsize;
}
 

int CCrawl::fetch(string strUrl, char **fileBuf , size_t &imgSize)
{

	CURL *curl;

	CURLcode res;

	struct MemoryStruct chunk;

	struct MemoryStruct DataChunk;


	chunk.memory = (char *)malloc(1);  /* will be grown as needed by the realloc above */ 
	chunk.size = 0;    /* no data at this point */ 


	DataChunk.memory = (char *)malloc(1);  /* will be grown as needed by the realloc above */ 
	DataChunk.size = 0;    /* no data at this point */ 

//	curl_global_init(CURL_GLOBAL_ALL);

	/* init the curl session */ 
	curl = curl_easy_init();

	/* specify URL to get */
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());

	/* complete within 20 seconds */

	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);


	/* send all data to this function  */

	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WriteMemoryCallback);

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);


	/* we pass our 'chunk' struct to the callback function */ 
	curl_easy_setopt(curl,  CURLOPT_WRITEHEADER, (void *)&chunk);

	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&DataChunk);

	/* some servers don't like requests that are made without a user-agent field, so we provide one */ 
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

	/* ge
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值