The use of libcurl library in Cocos2d-x (3) Introduction to HTTP

Source: Internet
Author: User
1. HTTP verification currently supports the following verification methods: basic, Digest, NTLM, Negotiate, GSS-Negotiate, and mongogo. You can set specific verification methods through the CURLOPT_HTTPAUTH attribute, for example: curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); when sending verification information to the proxy server,

1. HTTP verification currently supports the following verification methods: basic, Digest, NTLM, Negotiate, GSS-Negotiate, and mongogo. You can set specific verification methods through the CURLOPT_HTTPAUTH attribute, for example: curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); when sending verification information to the proxy server,

1. HTTP Verification

Currently, HTTP supports the following verification methods: basic, Digest, NTLM, Negotiate, GSS-Negotiate, and mongogo. You can set specific verification methods through the CURLOPT_HTTPAUTH attribute, for example, curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); when sending verification information to the proxy server, you can use CURLOPT_PROXYAUTH to set verification methods, or you can set multiple verification methods at the same time, using the CURLOPT_HTTPAUTH or CURLOPT_PROXYAUTH attributes. Curl_easy_setopt (easy_handle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM)

Libcurl selects the best way to communicate with the server at runtime.

2. HTTP Post OF libcurl

#include 
 
  #include 
  
   int main(void){  CURL *curl;  CURLcode res;  /* In windows, this will init the winsock stuff */  curl_global_init(CURL_GLOBAL_ALL);  /* get a curl handle */  curl = curl_easy_init();  if(curl) {    /* First set the URL that is about to receive our POST. This URL can       just as well be a https:// URL if that is what should receive the       data. */    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");    /* Now specify the POST data */    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");    /* Perform the request, res will get the return code */    res = curl_easy_perform(curl);    /* Check for errors */    if(res != CURLE_OK)      fprintf(stderr, "curl_easy_perform() failed: %s\n",              curl_easy_strerror(res));    /* always cleanup */    curl_easy_cleanup(curl);  }  curl_global_cleanup();  return 0;}
  
 

3. Multi Post of libcurl

#include 
 
  #include 
  
   #include 
   
    #include 
    
     int main(void){  CURL *curl;  CURLM *multi_handle;  int still_running;  struct curl_httppost *formpost=NULL;  struct curl_httppost *lastptr=NULL;  struct curl_slist *headerlist=NULL;  static const char buf[] = "Expect:";  /* Fill in the file upload field. This makes libcurl load data from     the given file name when curl_easy_perform() is called. */  curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "sendfile",               CURLFORM_FILE, "postit2.c",               CURLFORM_END);  /* Fill in the filename field */  curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "filename",               CURLFORM_COPYCONTENTS, "postit2.c",               CURLFORM_END);  /* Fill in the submit field too, even if this is rarely needed */  curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "submit",               CURLFORM_COPYCONTENTS, "send",               CURLFORM_END);  curl = curl_easy_init();  multi_handle = curl_multi_init();  /* initalize custom header list (stating that Expect: 100-continue is not     wanted */  headerlist = curl_slist_append(headerlist, buf);  if(curl && multi_handle) {    /* what URL that receives this POST */    curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);    curl_multi_add_handle(multi_handle, curl);    curl_multi_perform(multi_handle, &still_running);    do {      struct timeval timeout;      int rc; /* select() return code */      fd_set fdread;      fd_set fdwrite;      fd_set fdexcep;      int maxfd = -1;      long curl_timeo = -1;      FD_ZERO(&fdread);      FD_ZERO(&fdwrite);      FD_ZERO(&fdexcep);      /* set a suitable timeout to play around with */      timeout.tv_sec = 1;      timeout.tv_usec = 0;      curl_multi_timeout(multi_handle, &curl_timeo);      if(curl_timeo >= 0) {        timeout.tv_sec = curl_timeo / 1000;        if(timeout.tv_sec > 1)          timeout.tv_sec = 1;        else          timeout.tv_usec = (curl_timeo % 1000) * 1000;      }      /* get file descriptors from the transfers */      curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);      /* In a real-world program you OF COURSE check the return code of the         function calls.  On success, the value of maxfd is guaranteed to be         greater or equal than -1.  We call select(maxfd + 1, ...), specially in         case of (maxfd == -1), we call select(0, ...), which is basically equal         to sleep. */      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);      switch(rc) {      case -1:        /* select error */        break;      case 0:      default:        /* timeout or readable/writable sockets */        printf("perform!\n");        curl_multi_perform(multi_handle, &still_running);        printf("running: %d!\n", still_running);        break;      }    } while(still_running);    curl_multi_cleanup(multi_handle);    /* always cleanup */    curl_easy_cleanup(curl);    /* then cleanup the formpost chain */    curl_formfree(formpost);    /* free slist */    curl_slist_free_all (headerlist);  }  return 0;}
    
   
  
 

Multi Post can be a better way to submit binary data (or a large amount of data). The definitions are found in RFC1867 and RFC2388, and there are different data units in Post, each unit has its own name and content, the content can be text or binary, and each data unit can have its own message header. These data units form a linked list and are submitted to the HTTP server. You can also add different data units and then submit them to 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.