//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
利用libcurl下载图片
最新推荐文章于 2021-09-07 11:29:39 发布
本文详细介绍了如何利用libcurl库在程序中实现图片的下载功能,从设置URL到保存文件,步骤清晰,实践效果满意。
摘要由CSDN通过智能技术生成