Other languages are more convenient to use HTTP uploads. But C + + this almost, but fortunately, Linux has a Curl command line tool, this is an open source project, under the sub-project is Libcurl,curl is called this API implementation of a series of ftp,http upload and download functions, this library function is quite a lot. There are also many supported protocols. This allows you to use this library for HTTP uploads and downloads.
Of course, the API of this library has two kinds of interfaces, one is the Esay-------synchronous blocking mode. The other is multi, I did not study it, the following is easy interface, to write the sample code.
However, before learning the interface of this library, it is better to understand the difference between HTTP, especially the Get and post methods, the former involves querying the URL, which involves rewriting the URL. Of course, both get and post can transfer data to the server. Can not be directly understood according to their name, see the HTTP protocol for details. I found two good blog connections to understand HTTP-related content, very good, well written:
Http://www.cnblogs.com/devil-91/archive/2012/05/11/2495266.html
Http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html
File Upload class:
H file:
#ifndef qcurl_sender_h#define qcurl_sender_h#include <string> #include <curl/curl.h>class curlsender{ Public:curlsender (); ~curlsender (); bool IsValid () const;void seturl (const std::string& URL); bool Send (const std:: String &file);p rivate:std::string getfilenamefrompath (const std::string& path);p rivate:curl* m_hcurl;std:: String M_url;bool m_isvalid;}; #endif
. cpp Files
#include "QCurlSender.h" Curlsender::curlsender (): M_hcurl (nullptr), M_isvalid (false) {Curl_global_init (Curl_global _all); M_hcurl = Curl_easy_init (); if (m_hcurl) {m_isvalid = true;}} Curlsender::~curlsender () {if (M_hcurl) {curl_easy_cleanup (m_hcurl);} Curl_global_cleanup ();} BOOL Curlsender::isvalid () Const{return m_isvalid;} void Curlsender::seturl (const std::string& URL) {m_url = URL;} BOOL Curlsender::send (const std::string &file) {curl_slist* poptionlist = Null;poptionlist = Curl_slist_append ( Poptionlist, "Expect:"); Curl_easy_setopt (M_hcurl, Curlopt_httpheader, poptionlist); curl_httppost* pFormPost = NULL; curl_httppost* Plastelem = null;//upload file, specify local file full path Curl_formadd (&pformpost, &plastelem, Curlform_copyname, " Sendfile ", Curlform_file, File.c_str (), Curlform_contenttype," Application/octet-stream ", curlform_end); curl_ Formadd (&pformpost, &plastelem,curlform_copyname, "filename", curlform_copycontents, Getfilenamefrompath ( file). C_str (), curlform_end);//does not add an end to the HFS service side cannot write to files, there is generally no such problem, and it is added just for testing. Curl_formadd (&pformpost, &plastelem, Curlform_copyname, "End", curlform_copycontents , "End", Curlform_end); Curl_easy_setopt (M_hcurl, Curlopt_httppost, pformpost); Curl_easy_setopt (M_hCurl, CURLOPT_URL , M_url.c_str ()); Curlcode res = curl_easy_perform (m_hcurl); if (res! = CURLE_OK) {return false;} Curl_formfree (pformpost); return true;} std::string curlsender::getfilenamefrompath (const std::string& path) {return path.substr (path.find_last_of ("/\\ ") + 1);}
References
Http://www.cnblogs.com/cswuyg/archive/2013/07/11/3185164.html
Http://www.cnblogs.com/lidabo/p/4159574.html
http://blog.csdn.net/breaksoftware/article/details/45874197
Http://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
Upload and download files using HTTP