Skip to content

Instantly share code, notes, and snippets.

@ityuhui
Created February 24, 2016 06:42
Show Gist options
  • Save ityuhui/97c12ab582b86f6393cc to your computer and use it in GitHub Desktop.
Save ityuhui/97c12ab582b86f6393cc to your computer and use it in GitHub Desktop.
c++ libcurl 使用
// studyLibCurl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <curl/curl.h>
#include <iostream>
#include <string>
size_t offset = 0;
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
size_t realSize = size * nmemb;
memcpy((char *)userp + offset,buffer,realSize);
offset += realSize;
return realSize;
}
int main(int argc,char *argv[])
{
char content[CURL_MAX_WRITE_SIZE] = {0};
curl_global_init(CURL_GLOBAL_ALL);
CURL *easyhandle = curl_easy_init();
curl_easy_setopt(easyhandle, CURLOPT_URL, argv[1]);
curl_easy_setopt(easyhandle, CURLOPT_TIMEOUT, 20);
curl_easy_setopt(easyhandle, CURLOPT_USERAGENT, "yuhuiwebclient/0.1");
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);
size_t received = curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, (void *)content);
CURLcode success = curl_easy_perform(easyhandle);
curl_easy_cleanup(easyhandle);
printf("%s\n",content);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment