Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031
00032 #include <curl/curl.h>
00033
00034 struct MemoryStruct {
00035 char *memory;
00036 size_t size;
00037 };
00038
00039 static size_t
00040 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
00041 {
00042 size_t realsize = size * nmemb;
00043 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
00044
00045 mem->memory = realloc(mem->memory, mem->size + realsize + 1);
00046 if(mem->memory == NULL) {
00047
00048 printf("not enough memory (realloc returned NULL)\n");
00049 return 0;
00050 }
00051
00052 memcpy(&(mem->memory[mem->size]), contents, realsize);
00053 mem->size += realsize;
00054 mem->memory[mem->size] = 0;
00055
00056 return realsize;
00057 }
00058
00059 int main(void)
00060 {
00061 CURL *curl_handle;
00062 CURLcode res;
00063
00064 struct MemoryStruct chunk;
00065
00066 chunk.memory = malloc(1);
00067 chunk.size = 0;
00068
00069 curl_global_init(CURL_GLOBAL_ALL);
00070
00071
00072 curl_handle = curl_easy_init();
00073
00074
00075 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
00076
00077
00078 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00079
00080
00081 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
00082
00083
00084
00085 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
00086
00087
00088 res = curl_easy_perform(curl_handle);
00089
00090
00091 if(res != CURLE_OK) {
00092 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00093 curl_easy_strerror(res));
00094 }
00095 else {
00096
00097
00098
00099
00100
00101
00102
00103 printf("%lu bytes retrieved\n", (long)chunk.size);
00104 }
00105
00106
00107 curl_easy_cleanup(curl_handle);
00108
00109 free(chunk.memory);
00110
00111
00112 curl_global_cleanup();
00113
00114 return 0;
00115 }