C语言 HTTP上传文件-利用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

上一篇:(转) c/c++调用libcurl库发送http请求的两种基本用法


下一篇:【Android应用开发】Android Studio 简介 (Android Studio Overview)