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 #include <stdio.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030
00031 const char data[]="this is what we post to the silly web server";
00032
00033 struct WriteThis {
00034 const char *readptr;
00035 long sizeleft;
00036 };
00037
00038 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00039 {
00040 struct WriteThis *pooh = (struct WriteThis *)userp;
00041
00042 if(size*nmemb < 1)
00043 return 0;
00044
00045 if(pooh->sizeleft) {
00046 *(char *)ptr = pooh->readptr[0];
00047 pooh->readptr++;
00048 pooh->sizeleft--;
00049 return 1;
00050 }
00051
00052 return 0;
00053 }
00054
00055 int main(void)
00056 {
00057 CURL *curl;
00058 CURLcode res;
00059
00060 struct WriteThis pooh;
00061
00062 pooh.readptr = data;
00063 pooh.sizeleft = (long)strlen(data);
00064
00065
00066 res = curl_global_init(CURL_GLOBAL_DEFAULT);
00067
00068 if(res != CURLE_OK) {
00069 fprintf(stderr, "curl_global_init() failed: %s\n",
00070 curl_easy_strerror(res));
00071 return 1;
00072 }
00073
00074
00075 curl = curl_easy_init();
00076 if(curl) {
00077
00078 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/index.cgi");
00079
00080
00081 curl_easy_setopt(curl, CURLOPT_POST, 1L);
00082
00083
00084 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00085
00086
00087 curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
00088
00089
00090 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00091
00092
00093
00094
00095
00096
00097
00098
00099 #ifdef USE_CHUNKED
00100 {
00101 struct curl_slist *chunk = NULL;
00102
00103 chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
00104 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
00105
00106
00107 }
00108 #else
00109
00110
00111 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
00112 #endif
00113
00114 #ifdef DISABLE_EXPECT
00115
00116
00117
00118
00119
00120
00121
00122
00123 {
00124 struct curl_slist *chunk = NULL;
00125
00126 chunk = curl_slist_append(chunk, "Expect:");
00127 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
00128
00129
00130 }
00131 #endif
00132
00133
00134 res = curl_easy_perform(curl);
00135
00136 if(res != CURLE_OK)
00137 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00138 curl_easy_strerror(res));
00139
00140
00141 curl_easy_cleanup(curl);
00142 }
00143 curl_global_cleanup();
00144 return 0;
00145 }