receiving data after curl_easy_perform
I have the following question: how can i write data returning with http-response in char *
buffer? I've found several approaches:
- use
CURLOPT_WRITEDATA
orCURLOPT_WRITEFUNCTION
. butCURLOPT_WRITEDATA
requires file pointer (FILE *
). use ofCURLOPT_WRITEFUNCTION
with callback function seems to me as quirk... - use
curl_easy_send
andcurl_easy_recv
. but in this case i'll need to write allPOST
headers with hands...
Is开发者_JAVA百科 there some other, more elegant approach? e.g. pass char *
buffer pointer into some function to get http response in.
Actually CURLOPT_WRITEDATA and CURLOPT_WRITEFUNCTION can be used with any pointer type. As long as your function is compatible with that pointer type.
For example:
...
client_t *client;
CURL *conn;
...
curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, read_data);
curl_easy_setopt(conn, CURLOPT_WRITEDATA, client);
...
static size_t read_data(void *ptr,
size_t size,
size_t nmemb,
client_t *client)
{
memcpy(client->data, ptr, size * nmemb);
return size * nmemb;
}
精彩评论