69,373
社区成员
发帖
与我相关
我的任务
分享
#define MAX_BUF 65536
char wr_buf[MAX_BUF+1];
void test()
{
.....
if (curl) {
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "d:/cookie.txt"); // 指定cookie文件
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "d:/cookie.txt");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers );
curl_easy_setopt(curl, CURLOPT_URL, sign_url); // 指定url
curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&wr_error);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data );//调用写回调
/* Allow curl to perform the action */
res = curl_easy_perform(curl);
printf( "ret = %d (write_error = %d)\n", res, wr_error );
/* Emit the page if curl indicates that no errors occurred */
if ( res == 0 )
printf( "%s\n", wr_buf );
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
.....
}
/*
* Write data callback function (called within the context of
* curl_easy_perform.
*/
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) {
int segsize = size * nmemb;
/* Check to see if this data exceeds the size of our buffer. If so,
* set the user-defined context value and return 0 to indicate a
* problem to curl.
*/
if ( wr_index + segsize > MAX_BUF ) {
*(int *)userp = 1;
return 0;
}
/* Copy the data from the curl buffer into our buffer */
memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize );
/* Update the write index */
wr_index += segsize;
/* Null terminate the buffer */
wr_buf[wr_index] = 0;
/* Return the number of bytes received, indicating to curl that all is okay */
return segsize;
}