使用libcurl进行文件上传

简介: 上篇博文讲到了如何使用multicurl来进行http并发访问,今天继续有关curl的主题,来八一八如何使用curl来上传文件,在介绍具体方法之前了解下目前http文件上传的基本实现。     rfc1867描述了如何使用http协议来上传客户端文件,目前基本上所有的浏览器和web服务器都支持ht...

上篇博文讲到了如何使用multicurl来进行http并发访问,今天继续有关curl的主题,来八一八如何使用curl来上传文件,在介绍具体方法之前了解下目前http文件上传的基本实现。

    rfc1867描述了如何使用http协议来上传客户端文件,目前基本上所有的浏览器和web服务器都支持http文件上传,它的使用也十分的简单,具体的来说就是在页面上创建一个form表单,表单的enctype属性为multipart/form-data,action为接收上传文件的cgi url,请求方式为post,在表单中添加type属性为file的input,file input里面选择需要上传的文件,选择好后点击submit,服务器端收到multipart post请求后,会根据相关协议解析请求,然后保存上传的文件内容,Multipart表单示例:

[html]  view plain copy
 
  1. <form enctype="multipart/form-data" action="http://host:port/UploadFile" method=post>  
  2.     Upload :<br>  
  3.     <input name="userfile" type="file"><br>  
  4.     text field :<input type="text" name="text" value="text"><br>  
  5.     <input type="submit" value="提交"><input type=reset>  
  6. </form>  


    好了,现在来讲一讲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

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",   CURLFORM_COPYCONTENTS, "content", CURLFORM_END);  

2、添加name/content/contenttype section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",   CURLFORM_COPYCONTENTS, "content",   CURLFORM_CONTENTTYPE, "type", CURLFORM_END);  

3、添加 file/filename section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic",   CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_END);     

4、添加file/contenttype section

[cpp]  view plain copy
 
  1. 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官网上提供的文件上传例子:

[cpp]  view plain copy
 
    1. /* This is an example application source code using the multi interface 
    2.  * to do a multipart formpost without "blocking". */  
    3. #include <stdio.h>  
    4. #include <string.h>  
    5. #include <sys/time.h>  
    6.   
    7. #include <curl/curl.h>  
    8.   
    9. int main(void)  
    10. {  
    11.   CURL *curl;  
    12.   
    13.   CURLM *multi_handle;  
    14.   int still_running;  
    15.   
    16.   struct curl_httppost *formpost=NULL;  
    17.   struct curl_httppost *lastptr=NULL;  
    18.   struct curl_slist *headerlist=NULL;  
    19.   static const char buf[] = "Expect:";  
    20.   
    21.   /* Fill in the file upload field. This makes libcurl load data from 
    22.      the given file name when curl_easy_perform() is called. */  
    23.   curl_formadd(&formpost,  
    24.                &lastptr,  
    25.                CURLFORM_COPYNAME, "sendfile",  
    26.                CURLFORM_FILE, "postit2.c",  
    27.                CURLFORM_END);  
    28.   
    29.   /* Fill in the filename field */  
    30.   curl_formadd(&formpost,  
    31.                &lastptr,  
    32.                CURLFORM_COPYNAME, "filename",  
    33.                CURLFORM_COPYCONTENTS, "postit2.c",  
    34.                CURLFORM_END);  
    35.   
    36.   /* Fill in the submit field too, even if this is rarely needed */  
    37.   curl_formadd(&formpost,  
    38.                &lastptr,  
    39.                CURLFORM_COPYNAME, "submit",  
    40.                CURLFORM_COPYCONTENTS, "send",  
    41.                CURLFORM_END);  
    42.   
    43.   curl = curl_easy_init();  
    44.   multi_handle = curl_multi_init();  
    45.   
    46.   /* initalize custom header list (stating that Expect: 100-continue is not 
    47.      wanted */  
    48.   headerlist = curl_slist_append(headerlist, buf);  
    49.   if(curl && multi_handle) {  
    50.   
    51.     /* what URL that receives this POST */  
    52.     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");  
    53.     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);  
    54.   
    55.     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
    56.     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
    57.   
    58.     curl_multi_add_handle(multi_handle, curl);  
    59.   
    60.     curl_multi_perform(multi_handle, &still_running);  
    61.   
    62.     do {  
    63.       struct timeval timeout;  
    64.       int rc; /* select() return code */  
    65.   
    66.       fd_set fdread;  
    67.       fd_set fdwrite;  
    68.       fd_set fdexcep;  
    69.       int maxfd = -1;  
    70.   
    71.       long curl_timeo = -1;  
    72.   
    73.       FD_ZERO(&fdread);  
    74.       FD_ZERO(&fdwrite);  
    75.       FD_ZERO(&fdexcep);  
    76.   
    77.       /* set a suitable timeout to play around with */  
    78.       timeout.tv_sec = 1;  
    79.       timeout.tv_usec = 0;  
    80.   
    81.       curl_multi_timeout(multi_handle, &curl_timeo);  
    82.       if(curl_timeo >= 0) {  
    83.         timeout.tv_sec = curl_timeo / 1000;  
    84.         if(timeout.tv_sec > 1)  
    85.           timeout.tv_sec = 1;  
    86.         else  
    87.           timeout.tv_usec = (curl_timeo % 1000) * 1000;  
    88.       }  
    89.   
    90.       /* get file descriptors from the transfers */  
    91.       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);  
    92.   
    93.       /* In a real-world program you OF COURSE check the return code of the 
    94.          function calls.  On success, the value of maxfd is guaranteed to be 
    95.          greater or equal than -1.  We call select(maxfd + 1, ...), specially in 
    96.          case of (maxfd == -1), we call select(0, ...), which is basically equal 
    97.          to sleep. */  
    98.   
    99.       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);  
    100.   
    101.       switch(rc) {  
    102.       case -1:  
    103.         /* select error */  
    104.         break;  
    105.       case 0:  
    106.       default:  
    107.         /* timeout or readable/writable sockets */  
    108.         printf("perform!\n");  
    109.         curl_multi_perform(multi_handle, &still_running);  
    110.         printf("running: %d!\n", still_running);  
    111.         break;  
    112.       }  
    113.     } while(still_running);  
    114.   
    115.     curl_multi_cleanup(multi_handle);  
    116.   
    117.     /* always cleanup */  
    118.     curl_easy_cleanup(curl);  
    119.   
    120.     /* then cleanup the formpost chain */  
    121.     curl_formfree(formpost);  
    122.   
    123.     /* free slist */  
    124.     curl_slist_free_all (headerlist);  
    125.   }  
    126.   return 0;  
    127. }  
目录
相关文章
|
C语言
C语言 HTTP上传文件-利用libcurl库上传文件
原文  http://justwinit.cn/post/7626/ 通常情况下,一般很少使用C语言来直接上传文件,但是遇到使用C语言编程实现文件上传时,该怎么做呢? 借助开源的libcurl库,我们可以容易地实现这个功能。
3129 0
|
8月前
requests--文件上传,文件下载
requests--文件上传,文件下载
|
11月前
libcurl的post请求模板
libcurl的post请求模板
73 0
|
JavaScript 数据格式 XML
libcurl上传文件
libcurl参数很多,一不小心就容易遇到问题。曾经就遇到过一个很蛋疼的问题:libcurl断点下载>>   这里主要汇总一下,libcurl上传的二种方式: 1、直接上传文件,类似form表单,>   直接上传文件: struct curl_httppost *fo...
1417 0
|
测试技术 C++ Windows
Windows下VS2015编译并使用libcurl.lib静态库(curl:http客户端库c++版本)
Windows下VS2015编译并使用libcurl.lib静态库(curl:http客户端库c++版本)
1032 0
|
Web App开发 网络安全
|
PHP C++ 测试技术
libcurl 上传文件至 web服务器
测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php)    libcurl vc6 工程代码  下载地址:  http://download.csdn.
1063 0
|
C++
libcurl 通过http协议下载文件并显示下载进度
vc6 测试工程下载地址: http://download.csdn.net/detail/mtour/8068053   代码如下:     [cpp] view plain copy     size_t my_write_func(void *ptr, siz...
2586 0