就可以了,所以写了下面错误的代码
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 data curl_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; }
所以参考了curl库的例子
设置CURLOPT_READFUNCTION,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); }