HTTP GET request gets the reply data returned by the server

Source: Internet
Author: User

The number of parameters in the Libcurl library curlopt_writefunction The callback function that should be set:

size_t FUN_CB (char *ptr, size_t size, size_t nmemb, void *userdata)
The callback function is called when the response data arrives, and the data is pointed to by PTR, and the size is size*nmemb. So far it's a document. From the socket perspective, the response data will not necessarily be a string ending in 0. It should be thought of as streaming data. Only the service side does not close the connection, just to the service side is still sending the response data, this function will be called, and the number of calls is not necessarily only once, perhaps many times, each time the data received by the call is Size*nmemb, These documents do not seem to be mentioned, because this is the right thing to do. So when it comes to handling response data, you don't take it for granted, you need to consider that this function is called multiple times.

The callback function is called when the response data arrives, and the data is pointed to by PTR, and the size is size*nmemb. So far it's a document. From the socket perspective, the response data will not necessarily be a string ending in 0. It should be thought of as streaming data. Only the service side does not close the connection, just to the service side is still sending the response data, this function will be called, and the number of calls is not necessarily only once, perhaps many times, each time the data received by the call is Size*nmemb, These documents do not seem to be mentioned, because this is the right thing to do. So when it comes to handling response data, you don't take it for granted, you need to consider that this function is called multiple times.
say it again. UserData, this is a pointer to file * , which is related to Curlopt_writedata, assuming that you have written the callback function, instead of using the default callback function to write the received data to the curlopt _writedata the file to which the UserData is set, it is possible to set the pointer to null.

Example one:

  1. Char *res_buf = NULL;
  2. int shift;
  3. size_t copy_data(void *ptr, size_t size, size_t nmemb, void * Stream)
  4. {
  5. int res_size;

  6. Res_size = size * nmemb;
  7. Res_buf = realloc(res_buf, shift+res_size + 1);
  8. memcpy(res_buf + shift, ptr, res_size);
  9. Shift += res_size;
  10. Return size * nmemb;
  11. }
I don't know what that means for the following situations:

Example two:

Curl* Curl;
Curlcode Res;

Char buffer[10] ={0};

curl = Curl_easy_init ();//curl initialization
std::string _version;
if (Curl)
{
curl_easy_setopt (Curl, Curlopt_url, "Https://raw.github.com/minggo/AssetsManagerTest/master/version");curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, 0L);//set to No authentication certificate
curl_easy_setopt (Curl, curlopt_writefunction, getversioncode);//Set functions for processing data
 curl_easy_setopt (Curl, Curlopt_writedata, &_version);//Data store object pointer
res = curl_easy_perform (curl);//curl Link
curl_easy_cleanup (curl);//Clear Curl


}

I would like to ask, curlopt_writedata the specified should not be a file pointer. Why is it a string type?

For example one involves global variables, so in a multithreaded environment is not appropriate. The following example three does not involve global variables.

Example three:

# include <stdio.h> # include <stdlib.h> # include <string.h> # include <curl/curl.h> struct memorystruct {Char*memory; size_t size;};Staticsize_tWritememorycallback(void*contents, size_t size, size_t nmemb,void*USERP) {size_t realsize = size * NMEMB;structMemorystruct *mem = (structMemorystruct *) Userp; Mem->memory = ReAlloc (mem->memory, mem->size + realsize + 1);if(mem->memory = = NULL) {/* Out of memory! * /printf"Not enough memory (ReAlloc returned NULL) \ n");return0;  } memcpy (& (Mem->memory[mem->size]), contents, realsize);  Mem->size + = Realsize; Mem->memory[mem->size] = 0;returnRealsize;} int main (void) {CURL *curl_handle; Curlcode Res;structMemorystruct Chunk;    Chunk.memory = malloc (1);      chunk.size = 0;   Curl_global_init (Curl_global_all);   Curl_handle = Curl_easy_init (); Curl_easy_setopt (Curl_handle, Curlopt_url,"http://www.example.com/");   Curl_easy_setopt (Curl_handle, curlopt_writefunction, Writememorycallback); Curl_easy_setopt (Curl_handle, Curlopt_writedata, (void*) &chunk); Curl_easy_setopt (Curl_handle, Curlopt_useragent,"libcurl-agent/1.0"); res = Curl_easy_perform (curl_handle);if(res! = CURLE_OK) {fprintf (stderr,"Curl_easy_perform () failed:%s\n", Curl_easy_strerror (res)); }Else{ printf"%lu bytes retrieved\n", (Long) chunk.size);   } curl_easy_cleanup (Curl_handle); Free (chunk.memory); Curl_global_cleanup ();return0;}

HTTP GET request gets the reply data returned by the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.