C语言 HTTP上传文件-利用libcurl库上传文件

简介: 原文  http://justwinit.cn/post/7626/ 通常情况下,一般很少使用C语言来直接上传文件,但是遇到使用C语言编程实现文件上传时,该怎么做呢? 借助开源的libcurl库,我们可以容易地实现这个功能。
+关注继续查看

通常情况下,一般很少使用C语言来直接上传文件,但是遇到使用C语言编程实现文件上传时,该怎么做呢?

借助开源的libcurl库,我们可以容易地实现这个功能。Libcurl是一个免费易用的客户端URL传输库,主要功能是用不同的协议连接和沟通不同的服务器,libcurl当前支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP,IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet andTFTP。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证等。下面借鉴libcurl官网的例子完成简单的文件上传。

模拟要实现的文件上传FORM:

 

File: FileName:

 

 

 

  1. <form action="fileUpload.action" method="post" enctype="multipart/form-data">    
  2.         File:<input type="file" name="sendfile" /><br>     
  3.         FileName:<input type="text" name="filename" /><br>     
  4.         <input type="submit" name="submit" value="Submit" />    
  5. </form> 

其中,fileUpload.action为文件处理文件上传的接口,根据实际需要配置,这里只是一个例子。

C语言HTTP上传文件的代码如下:

#include <stdio.h>  

#include <string.h>    

#include <curl/curl.h>

  
int main(int argc, char *argv[])  
{  
  CURL *curl;  
  CURLcode res;  
  
  struct curl_httppost *formpost=NULL;  
  struct curl_httppost *lastptr=NULL;  
  struct curl_slist *headerlist=NULL;  
  static const char buf[] = "Expect:";  
  
  curl_global_init(CURL_GLOBAL_ALL);  
  
  /* Fill in the file upload field */  
  curl_formadd(&formpost,  
               &lastptr,  
               CURLFORM_COPYNAME, "sendfile",  
               CURLFORM_FILE, "D:\\sign.txt",  
               CURLFORM_END);  
  
  /* Fill in the filename field */  
  curl_formadd(&formpost,  
               &lastptr,  
               CURLFORM_COPYNAME, "filename",  
               CURLFORM_COPYCONTENTS, "sign.txt",  
               CURLFORM_END);  
  
  /* Fill in the submit field too, even if this is rarely needed */  
  curl_formadd(&formpost,  
               &lastptr,  
               CURLFORM_COPYNAME, "submit",  
               CURLFORM_COPYCONTENTS, "Submit",  
               CURLFORM_END);  
  
  curl = curl_easy_init();  
  /* initalize custom header list (stating that Expect: 100-continue is not 
     wanted */  
  headerlist = curl_slist_append(headerlist, buf);  
  if(curl) {  
    /* what URL that receives this POST */  

     curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/fileUpload.action");  
    if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )  
      /* only disable 100-continue header if explicitly requested */  
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
  
    /* 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);  
  
    /* then cleanup the formpost chain */  
    curl_formfree(formpost);  
    /* free slist */  
    curl_slist_free_all (headerlist);  
  }  
  return 0;  
}  
  
 

代码经过测试,可以使用,但是需要提前配置好Libcur库,以及编译环境,这个自行google。代码很粗糙,功能很简单,只是起个抛砖引玉的作用,希望能对大家有所帮助。

来自:http://blog.csdn.net/sxwyf248/article/details/7984776

VC2013下,使用curl:

http://www.tuicool.com/articles/A73ARr

较细:http://blog.csdn.net/mjpassion/article/details/6290912

Visual2012:http://www.howzhi.com/course/3387/lesson/43112

参考:http://www.cppblog.com/len/archive/2008/06/21/54229.html

使用libcurl模拟form表单上传的问题:

http://bbs.csdn.net/topics/390817077

在C语言程序中使用cURL库(libcurl):

http://demon.tw/programming/c-libcurl.html

目录
相关文章
|
8天前
|
C语言
C语言string库之常用字符和字符串函数详解
C语言string库之常用字符和字符串函数详解
|
11天前
|
资源调度 JavaScript 前端开发
深入了解Axios:现代JavaScript中的HTTP请求库
Axios是一款流行的JavaScript HTTP请求库,广泛用于浏览器和Node.js环境中。它提供了一个简单而强大的方式来处理HTTP请求和响应,使开发者能够轻松地与服务器通信。在本博客中,我们将深入研究Axios的功能、用法、请求配置、拦截器以及如何在现代Web开发中充分利用这个工具。
16 0
|
23天前
|
测试技术 C语言
分享一个好用的C语言.ini文件的解析库
分享一个好用的C语言.ini文件的解析库
30 0
|
2月前
|
JSON 数据格式 Python
【从零学习python 】92.使用Python的requests库发送HTTP请求和处理响应
【从零学习python 】92.使用Python的requests库发送HTTP请求和处理响应
35 1
|
2月前
|
定位技术 C语言 C++
C语言书写推箱子(坤坤版easyx库)
C语言书写推箱子(坤坤版easyx库)
|
5月前
|
C语言
C语言正则匹配库(regex.h)
C语言正则匹配库(regex.h)
127 0
|
6月前
|
Go
Golang:grequests库-一个类似Requests的http客户端
Golang:grequests库-一个类似Requests的http客户端
140 0
|
6月前
|
JSON 数据格式 Python
如何在Python中使用requests库发送HTTP请求
本文将介绍如何在Python中使用requests库发送HTTP请求。requests是一个常用的Python HTTP库,可以方便地发送HTTP请求、处理响应数据等操作。本文将分别介绍requests库的基本使用方法、如何发送GET请求、如何发送POST请求以及如何使用requests库处理响应数据。
154 0
|
8月前
|
C语言 Python
一个高效的C语言命令行解析库
一个高效的C语言命令行解析库
162 0
|
10月前
|
编解码 Dart 网络协议
Flutter(十九)——网络编程:HttpClient与http库
Flutter(十九)——网络编程:HttpClient与http库
207 1
相关产品
云迁移中心
推荐文章
更多