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,"filename",CURLFORM_copYCONTENTS,CURLFORM_END); /* Fill in the submit fIEld too,even if this is rarely needed */ curl_formadd(&formpost,"submit","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,"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,&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服务器,也可以添加不同的数据单元,然后提交到服务器。 总结
以上是内存溢出为你收集整理的Cocos2d-x中libcurl库的使用(3)HTTP的介绍全部内容,希望文章能够帮你解决Cocos2d-x中libcurl库的使用(3)HTTP的介绍所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)