视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Cocos2d-x中libcurl库的使用(3)HTTP的介绍
2020-11-09 08:32:10 责编:小采
文档


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

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

#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、libcurl之Multi Post

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

下载本文
显示全文
专题