Skip to content

Instantly share code, notes, and snippets.

@axgle
Created October 28, 2009 12:49
Show Gist options
  • Save axgle/220460 to your computer and use it in GitHub Desktop.
Save axgle/220460 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
char *g_data;
size_t g_size;
size_t savedata(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t total = (size * nmemb);
char *temp = (char *)realloc(g_data, g_size + total + 1);
if(!temp){
free(g_data);
g_data = NULL;
g_size = 0;
return 0;
}
g_data = temp;
memcpy(g_data + g_size, ptr, total);
g_size += total;
g_data[g_size] = 0;
return total;
}
char *curl_request(char *url)
{
CURL *curl = curl_easy_init();
if(!curl) return NULL;
g_data = NULL;
g_size = 0;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, savedata);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HEADER);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
return g_data;
}
int main(int argc, char **argv)
{
char *google = curl_request("http://www.douban.com");
if(google){
printf("%s\n", google);
free(google);
}
return 0;
}
//http://www.rohitab.com/discuss/index.php?showtopic=27265
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment