Cocos2d-x中libcurl库的使用(3)HTTP的介绍

来源:互联网 发布:himall2.4完整版源码 编辑:程序博客网 时间:2024/04/30 15:38

1、HTTP验证

HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);向代理服务器发送验证信息时,可以通过CURLOPT_PROXYAUTH设置验证方式,也可以同时设置多种验证方式,通过CURLOPT_HTTPAUTH或CURLOPT_PROXYAUTH属性设置多种验证方式。curl_easy_setopt(easy_handle,CURLOPT_PROXYAUTH,CURLAUTH_NTLM)

libcurl在运行时会选择一种它认为是最好的方式与服务器通信。

2、libcurl之HTTP Post

使用post方式向服务器提交数据,通过html中使用<form>标签提交数据一样,只需向libcurl提供一个包含数据的字符串即可。下面是libcurl库中提供的demo
#include <stdio.h>#include <curl/curl.h>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、libcurl之Multi Post

#include <stdio.h>#include <string.h>#include <sys/time.h>#include <curl/curl.h>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可以提交二进制数据(或大量数据)的更好的方法,在RFC1867、RFC2388找到定义,在Post时,有不同的数据单元,每个单元有自己的名称与内容,内容可以是文本的,也可以是二进制的,同时,每个数据单元都可以有自己的消息头。这些数据单元组成一个链表,提交到HTTP服务器,也可以添加不同的数据单元,然后提交到服务器。
0 0
原创粉丝点击