最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-09 08:32:10
文档

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

Cocos2d-x中libcurl库的使用(3)HTTP的介绍:1、HTTP验证 HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAU
推荐度:
导读Cocos2d-x中libcurl库的使用(3)HTTP的介绍:1、HTTP验证 HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAU

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服务器,也可以添加不同的数据单元,然后提交到服务器。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

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

Cocos2d-x中libcurl库的使用(3)HTTP的介绍:1、HTTP验证 HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAU
推荐度:
标签: 使用 介绍 使用的
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top