当前位置: 技术问答>linux和unix
curl通过post向服务器请求数据,服务器返回的数据量超过1024个字节。客户端接收不完整。
来源: 互联网 发布时间:2017-05-27
本文导语: 客户端通过post向服务器请求数据,服务器返回的数据达到3K, 客户端接收不完整。 请问有什么办法解决吗? 服务器返回的是一个json文件内容,不完整的话就没法解析了。 memset( curl_buffer, 0, 2048 ); cu...
客户端通过post向服务器请求数据,服务器返回的数据达到3K, 客户端接收不完整。
请问有什么办法解决吗?
服务器返回的是一个json文件内容,不完整的话就没法解析了。
memset( curl_buffer, 0, 2048 );
curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, 1);
curl_easy_setopt(m_curl, CURLOPT_URL, url);
curl_easy_setopt( m_curl, CURLOPT_POSTFIELDS, post_data );//data to server
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, write_callback);//callback func, write data
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, curl_buffer);//recv buffer
请问有什么办法解决吗?
服务器返回的是一个json文件内容,不完整的话就没法解析了。
memset( curl_buffer, 0, 2048 );
curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, 1);
curl_easy_setopt(m_curl, CURLOPT_URL, url);
curl_easy_setopt( m_curl, CURLOPT_POSTFIELDS, post_data );//data to server
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, write_callback);//callback func, write data
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, curl_buffer);//recv buffer
|
如果你的 curl_buffer 只是一个 char *的话,当然不行啦,你的回调函数会被多次调用的。
如果要直接保存到文件,直接用fwrite和FILE *fp设置CURLOPT_WRITEFUNCTION和CURLOPT_WRITEDATA
如果要保存到内存,请参考这段代码
http://curl.haxx.se/libcurl/c/getinmemory.html
如果要直接保存到文件,直接用fwrite和FILE *fp设置CURLOPT_WRITEFUNCTION和CURLOPT_WRITEDATA
如果要保存到内存,请参考这段代码
http://curl.haxx.se/libcurl/c/getinmemory.html
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ | |
* / __| | | | |_) | |
* | (__| |_| | _ memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrievedn", (long)chunk.size);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
if(chunk.memory)
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}