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