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 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030
00031 struct MemoryStruct {
00032 char *memory;
00033 size_t size;
00034 };
00035
00036 static size_t
00037 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
00038 {
00039 size_t realsize = size * nmemb;
00040 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
00041
00042 mem->memory = realloc(mem->memory, mem->size + realsize + 1);
00043 if(mem->memory == NULL) {
00044
00045 printf("not enough memory (realloc returned NULL)\n");
00046 return 0;
00047 }
00048
00049 memcpy(&(mem->memory[mem->size]), contents, realsize);
00050 mem->size += realsize;
00051 mem->memory[mem->size] = 0;
00052
00053 return realsize;
00054 }
00055
00056 int main(void)
00057 {
00058 CURL *curl;
00059 CURLcode res;
00060 struct MemoryStruct chunk;
00061 static const char *postthis="Field=1&Field=2&Field=3";
00062
00063 chunk.memory = malloc(1);
00064 chunk.size = 0;
00065
00066 curl_global_init(CURL_GLOBAL_ALL);
00067 curl = curl_easy_init();
00068 if(curl) {
00069
00070 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.org/");
00071
00072
00073 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00074
00075
00076 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
00077
00078
00079
00080 curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
00081
00082 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
00083
00084
00085
00086 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
00087
00088
00089 res = curl_easy_perform(curl);
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 printf("%s\n",chunk.memory);
00103 }
00104
00105
00106 curl_easy_cleanup(curl);
00107
00108 free(chunk.memory);
00109
00110
00111 curl_global_cleanup();
00112 }
00113 return 0;
00114 }