C language HTTP Upload file-upload file using Libcurl Library

Source: Internet
Author: User
Tags http post

Original http://justwinit.cn/post/7626/

Usually, it is seldom used in C to upload files directly, but how do you do it when using C language programming to implement file uploads?

With the open source Libcurl library, we can easily implement this feature. Libcurl is a free and easy to use client URL Transfer Library, the main function is to connect and communicate different servers with different protocols, Libcurl currently supports Dict, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, Imap,imaps, LDAP, LDAPS, POP3, Pop3s, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet andtftp. Libcurl also supports HTTPS certificate authorization, HTTP POST, http PUT, FTP upload, HTTP basic form upload, agent, cookies, and user authentication. Follow the example of Libcurl official website to complete a simple file upload.

Simulate the file upload form to be implemented:

Which, fileupload.action for file processing file upload interface, according to the actual need to configure, here is just an example.

The code for the C language HTTP upload file is as follows:

#include#include#includeint main (int argc,Char *argv[]) {CURL *curl; Curlcode Res;struct Curl_httppost *formpost=null;struct Curl_httppost *lastptr=null;struct Curl_slist *headerlist=null;StaticConstChar 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_copyn Ame"Submit", curlform_copycontents,"Submit", curlform_end); Curl = Curl_easy_init ();/* Initalize custom Header list (stating that expect:100-continue are not wanted */headerlist = Curl_slist_append (Headerl ist, buf);if (Curl) {/* What URL is receives this POST */curl_easy_setopt (curl, Curlopt_url,"Http://localhost:8080/fileUpload.action");if ((argc = =2) && (!strcmp (Argv[1], /* only disable 100-continue header if explicitly requested/curl_easy_setopt (curl, Curlopt_htt Pheader, headerlist); Curl_easy_setopt (Curl, Curlopt_httppost, formpost); /* Perform the request, res'll 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;}           

The code has been tested and can be used, but it needs to be configured in advance with the Libcur library, as well as the build environment, this self-Google. Code is very rough, the function is very simple, just play a role, I hope to be helpful to everyone.

From: http://blog.csdn.net/sxwyf248/article/details/7984776

Under VC2013, use curl:

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

Thinner: http://blog.csdn.net/mjpassion/article/details/6290912

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

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

To simulate the upload of form forms using Libcurl:

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

Using the Curl Library (Libcurl) in a C language program:

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

C language HTTP Upload file-upload file using Libcurl Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.