Curllib library http chunk sending method error correction
I used to think that to send chunk data, as long as the http header is added
Transfer-Encoding: chunked
So I wrote the following error code.
static int send_request(const char *req, char **data, int req_length){struct curl_slist *phttp_headers = NULL;CURL* ins_curl = NULL;//make_http_headers(&phttp_headers);ins_curl = curl_easy_init();if (ins_curl == NULL){printf("init curl failed\n");return -1;}phttp_headers = curl_slist_append(phttp_headers, "Transfer-Encoding: chunked");curl_slist_append(phttp_headers, "Content-Encoding: gzip");curl_easy_setopt(ins_curl, CURLOPT_URL, DEFAULT_SERVER);curl_easy_setopt(ins_curl, CURLOPT_WRITEFUNCTION, read_response_data);curl_easy_setopt(ins_curl, CURLOPT_POST, 1L);curl_easy_setopt(ins_curl, CURLOPT_VERBOSE, 1L);curl_easy_setopt(ins_curl, CURLOPT_HEADER, 1L);curl_easy_setopt(ins_curl, CURLOPT_FOLLOWLOCATION, 1L);// setup chunk and post datacurl_easy_setopt(ins_curl, CURLOPT_HTTPHEADER, phttp_headers);curl_easy_setopt(ins_curl, CURLOPT_POSTFIELDS, req);curl_easy_setopt(ins_curl, CURLOPT_POSTFIELDSIZE, req_length);if(curl_easy_perform(ins_curl) != CURLE_OK){printf("http transform error\n");}curl_easy_cleanup(ins_curl);return 1;}
The above error is:
CURLOPT_POSTFIELDS and CURLOPT_POSTFIELDSIZE are set.
After this setting, the chunk is not used for sending, but the normal method is used.
So refer to the curl library example
Set CURLOPT_READFUNCTION and CURLOPT_READDATA
/* We want to use our own read function */
Curl_easy_setopt (curl, CURLOPT_READFUNCTION, read_callback );
/* Pointer to pass to our read function */
Curl_easy_setopt (curl, CURLOPT_READDATA, & pooh );
size_t my_read_func(void *ptr, size_t size, size_t nmemb, void *stream){FILE *fp = (FILE *)stream;return fread(ptr, size, nmemb, fp);}
Then, the server will receive the message.
In addition, I think it's a coincidence to write back the above.
Refer:
Example post-callback.c In the curl Library
Http://hi.baidu.com/zhujinyu/blog/item/464522f42f634664dcc4747b.html
Http://blog.sina.com.cn/s/blog_78cf66b10100rvuw.html
Http://blog.chinaunix.net/link.php? Url = http://curl.haxx.se 4102flibcurl%2fc%2fcurl_easy_setopt.html % 23 curloptreaddata