开发者

receiving data after curl_easy_perform

开发者 https://www.devze.com 2023-02-01 13:44 出处:网络
I have the following question: how can i write data returning with http-response in char * buffer? I\'ve found several approaches:

I have the following question: how can i write data returning with http-response in char * buffer? I've found several approaches:

  1. use CURLOPT_WRITEDATA or CURLOPT_WRITEFUNCTION. but CURLOPT_WRITEDATA requires file pointer (FILE *). use of CURLOPT_WRITEFUNCTION with callback function seems to me as quirk...
  2. use curl_easy_send and curl_easy_recv. but in this case i'll need to write all POST 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消