使用libcurl进行文件、数据上传
来源:互联网 发布:kiki队长网络红人主播 编辑:程序博客网 时间:2024/05/19 00:37
好了,现在来讲一讲curl的文件上传,对于curl来讲,其实它要完成的任务就是构建一个multipart/formdata HTTP POST请求。类似于往multipart form表单中添加type为file或者text的input item一样,curl也需要我们构造表单中的input item,curl_formadd函数可以帮助我们完成这个任务,它即可以添加普通的name-value section,也可以添加file upload section,下面举几个具体例子:
1、添加name/content section
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
2、添加name/content/contenttype section
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_CONTENTTYPE, "type", CURLFORM_END);
3、添加 file/filename section
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_END);
4、添加file/contenttype section
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
上面的post 和 last都是指向curl_httppost对象的指针, post指向的就是一个由所有section组成的链表的开端,last是该链表的尾指针。当我们添加完所有的form section之后,使用curl_easy_setopt(curl, CURLOPT_HTTPPOST,post)函数设置curl的http post,最后就是调用curl_easy_perform执行请求。需要注意的是,当使用libcurl的POST方式时,如果POST数据的大小大于1024个字节,libcurl不会直接发送POST请求,而是会分为两步执行请求:
1、发送一个请求,该请求头部包含一个Expect: 100-continue的字段,用来询问server是否愿意接受数据
2、当接收到从server返回的100-continue的应答后,它才会真正的发起POST请求,将数据发送给server。
对于文件上传来说,文件大小往往会超过1024个字节,所以如果你确认你的服务器不会拒绝你的文件上传请求的话,可以禁止curl的Expect请求头,具体方法可以去看看我的另外一篇文章《libcurl的使用问题“Expect100-continue” 》。
最后附上curl官网上提供的文件上传例子:
- /* This is an example application source code using the multi interface
- * to do a multipart formpost without "blocking". */
- #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;
- }
- 使用libcurl进行文件、数据上传
- 使用libcurl进行文件上传
- 使用libcurl进行文件上传
- 使用libcurl POST数据和上传文件
- 使用libcurl POST数据和上传文件
- 使用libcurl POST数据和上传文件
- 使用libcurl上传文件
- 使用libcurl进行异步并发访问与文件上传
- 通过使用libcurl POST数据和上传与下载文件
- libCurl的文件上传
- libcurl post上传文件
- libcurl 上传文件
- 基于libcurl,使用PUT方法上传内存数据
- 使用libcurl进行文件下载类项目开发(一) libcurl简单使用介绍
- linux下c语言使用libcurl和实现文件上传
- Linux下使用libcurl库实现ftp上传文件
- 使用smartUpload组件进行文件上传以及获取文本数据
- spring不使用插件进行文件上传返回数据
- 数学之路-分布式计算-storm(1)
- 几个开关效果的CSS实现
- 软件工程师应该知道的9件事
- aaa
- Gradle DSL method not found: 'runProguard()'
- 使用libcurl进行文件、数据上传
- UDP sockt创建错误,初始化网络
- 试编写一个汇编语言程序,大写---小写 要求对键盘输入的大写字母用小写字母显示出来。
- 笔记1
- CSS 禁止选择(取消被选中背景)
- WTL工程设置:在X64环境中找不到atlres.h和atlapp.h
- windows下安装zookeeper
- EditText输入密码的显示与隐藏
- arcgis for js开发,根据给定的经纬度在地图上进行标注