Upload and download files over HTTP

Source: Internet
Author: User

Upload and download files over HTTP

It can be used on both PC and wce. It uses the MFC Library and has passed the test. Remember to add the header file # include <afxinet. h>

Download file:

Bool download (const cstring & strfileurlinserver, // URL of the file to be downloaded <br/> const cstring & strfilelocalfullpath) // local path <br/>{< br/> assert (strfileurlinserver! = ""); <Br/> assert (strfilelocalfullpath! = ""); <Br/> cinternetsession session; <br/> chttpconnection * phttpconnection = NULL; <br/> chttpfile * phttpfile = NULL; <br/> cstring strserver, strobject; <br/> internet_port wport; </P> <p> DWORD dwtype; <br/> const int ntimeout = 2000; <br/> session. setoption (internet_option_connect_timeout, ntimeout); // waiting delay between retries <br/> session. setoption (internet_option_connect_retries, 1); // Number of Retries <br/> char * psz Buffer = NULL; </P> <p> try <br/> {<br/> afxparseurl (strfileurlinserver, dwtype, strserver, strobject, wport ); <br/> phttpconnection = session. gethttpconnection (strserver, wport); <br/> phttpfile = phttpconnection-> openrequest (chttpconnection: http_verb_get, strobject); <br/> If (phttpfile-> sendrequest () = false) <br/> return false; <br/> DWORD dwstatecode; </P> <p> phttpfile-> queryinfostatuscode (DW Statecode); <br/> If (dwstatecode = http_status_ OK) <br/> {<br/> handle hfile = createfile (strfilelocalfullpath, generic_write, <br/> file_pai_write, null, create_always, file_attribute_normal, <br/> null); // create a local file <br/> If (hfile = invalid_handle_value) <br/>{< br/> phttpfile-> close (); <br/> phttpconnection-> close (); <br/> session. close (); <br/> return false; <br/>}</P> <p> char szinfobuffer [1000]; // return message <br/> DWORD dwfilesize = 0; // file length <br/> DWORD dwinfobuffersize = sizeof (szinfobuffer ); <br/> bool bresult = false; <br/> bresult = phttpfile-> queryinfo (http_query_content_length, <br/> (void *) szinfobuffer, & dwinfobuffersize, null ); </P> <p> dwfilesize = atoi (szinfobuffer); <br/> const int buffer_length = 1024*10; <br/> pszbuffer = new char [buffer_length]; // buffer for reading files <br/> DWORD dwwrit E, dwtotalwrite; <br/> dwwrite = dwtotalwrite = 0; <br/> uint nread = phttpfile-> Read (pszbuffer, buffer_length ); // read data on the server </P> <p> while (nread> 0) <br/>{< br/> writefile (hfile, pszbuffer, nread, & dwwrite, null); // write to the local file <br/> dwtotalwrite + = dwwrite; <br/> nread = phttpfile-> Read (pszbuffer, buffer_length ); <br/>}</P> <p> Delete [] pszbuffer; <br/> pszbuffer = NULL; <br/> closehandle (hfile); <br/>} <Br/>}< br/> catch (cinternetexception * E) <br/>{< br/> tchar tszerrstring [256]; <br/> E-> geterrormessage (tszerrstring, sizeof (tszerrstring); <br/> trace (_ T ("Download XSL error! URL: % s, error: % s "), strfileurlinserver, tszerrstring); <br/> E-> Delete (); <br/>}< br/> catch (...) <br/>{< br/>}</P> <p> If (pszbuffer! = NULL) <br/>{< br/> Delete [] pszbuffer; <br/> pszbuffer = NULL; <br/>}</P> <p> If (phttpfile! = NULL) <br/>{< br/> phttpfile-> close (); <br/> Delete phttpfile; <br/> phttpfile = NULL; <br/>}< br/> If (phttpconnection! = NULL) <br/>{< br/> phttpconnection-> close (); <br/> Delete phttpconnection; <br/> phttpconnection = NULL; <br/>}< br/> session. close (); <br/> return true; <br/>}< br/>

 

Test code:

Cstring urlinserver; <br/> cstring localfullpath; <br/> urlinserver = "http://www.163.com/photo"; <br/> localfullpath = "// test.html"; <br/> download (urlinserver, localfullpath); <br/>

 

After modification, the code

 

Bool chttpclient: Download (const string & strfileurlinserver, // URL of the file to be downloaded <br/> const string & strfilelocalfullpath) // local path <br/>{< br/> assert (strfileurlinserver! = ""); <Br/> assert (strfilelocalfullpath! = ""); <Br/> cinternetsession session; <br/> chttpconnection * phttpconnection = NULL; <br/> chttpfile * phttpfile = NULL; <br/> cstring strserver, strobject; <br/> internet_port wport; </P> <p> DWORD dwtype; <br/> const int ntimeout = 2000; <br/> session. setoption (internet_option_connect_timeout, ntimeout); // waiting delay between retries <br/> session. setoption (internet_option_connect_retries, 1); // Number of Retries <br/> char * psz Buffer = NULL; </P> <p> try <br/>{< br/> cstring TMP; <br/> TMP. format ("% s", strfileurlinserver. c_str (); <br/> afxparseurl (TMP, dwtype, strserver, strobject, wport); <br/> phttpconnection = session. gethttpconnection (strserver, wport); <br/> phttpfile = phttpconnection-> openrequest (chttpconnection: http_verb_get, strobject); <br/> If (phttpfile-> sendrequest () = false) <br/> return false; <br/> dwor D dwstatecode; </P> <p> phttpfile-> queryinfostatuscode (dwstatecode); <br/> If (dwstatecode = http_status_ OK) <br/>{< br/> // cstring localfullpath; <br/> TMP. format ("% s", strfilelocalfullpath. c_str (); <br/> handle hfile = createfile (TMP, generic_write, <br/> file_share_write, null, create_always, file_attribute_normal, <br/> null ); // create a local file <br/> If (hfile = invalid_handle_value) <br/>{< br/> phttp File-> close (); <br/> phttpconnection-> close (); <br/> session. close (); <br/> return false; <br/>}</P> <p> char szinfobuffer [1000]; // return message <br/> DWORD dwfilesize = 0; // file length <br/> DWORD dwinfobuffersize = sizeof (szinfobuffer); <br/> bool bresult = false; <br/> bresult = phttpfile-> queryinfo (http_query_content_length, <br/> (void *) szinfobuffer, & dwinfobuffersize, null ); </P> <p> dwfilesize = atoi (szinfob Uffer); <br/> const int buffer_length = 1024*10; <br/> pszbuffer = new char [buffer_length]; // buffer for reading files <br/> DWORD dwwrite, dwtotalwrite; <br/> dwwrite = dwtotalwrite = 0; <br/> uint nread = phttpfile-> Read (pszbuffer, buffer_length ); // read data on the server </P> <p> while (nread> 0) <br/>{< br/> writefile (hfile, pszbuffer, nread, & dwwrite, null); // write to a local file <br/> dwtotalwrite + = dwwrite; <br/> nread = phttpfile-> Read (pszbuffer, buffer_length); <br/>}</P> <p> Delete [] pszbuffer; <br/> pszbuffer = NULL; <br/> closehandle (hfile); <br/>}< br/> else <br/>{< br/> Delete [] pszbuffer; <br/> pszbuffer = NULL; <br/> If (phttpfile! = NULL) <br/>{< br/> phttpfile-> close (); <br/> Delete phttpfile; <br/> phttpfile = NULL; <br/>}< br/> If (phttpconnection! = NULL) <br/>{< br/> phttpconnection-> close (); <br/> Delete phttpconnection; <br/> phttpconnection = NULL; <br/>}< br/> session. close (); <br/> return false; <br/>}< br/> catch (...) <br/>{< br/> Delete [] pszbuffer; <br/> pszbuffer = NULL; <br/> If (phttpfile! = NULL) <br/>{< br/> phttpfile-> close (); <br/> Delete phttpfile; <br/> phttpfile = NULL; <br/>}< br/> If (phttpconnection! = NULL) <br/>{< br/> phttpconnection-> close (); <br/> Delete phttpconnection; <br/> phttpconnection = NULL; <br/>}< br/> session. close (); <br/> return false; <br/>}</P> <p> If (phttpfile! = NULL) <br/>{< br/> phttpfile-> close (); <br/> Delete phttpfile; <br/> phttpfile = NULL; <br/>}< br/> If (phttpconnection! = NULL) <br/>{< br/> phttpconnection-> close (); <br/> Delete phttpconnection; <br/> phttpconnection = NULL; <br/>}< br/> session. close (); <br/> return true; <br/>}< br/>

 

Upload files:

Uploadfile (lpctstr strurl, // URL of the page responsible for receiving upload operations <br/> lpctstr strlocalfilename) // path of the local file to be uploaded <br/>{< br/> assert (strurl! = NULL & strlocalfilename! = NULL); <br/> bool bresult = false; <br/> DWORD dwtype = 0; <br/> cstring strserver; <br/> cstring strobject; <br/> internet_port wport = 0; <br/> DWORD dwfilelength = 0; <br/> char * pfilebuff = NULL; </P> <p> chttpconnection * Primary privilege = NULL; <br/> chttpfile * phf = NULL; <br/> cinternetsession CIS; </P> <p> bresult = afxparseurl (strurl, dwtype, strserver, strobject, wport); <br/> If (! Bresult) <br/> return false; <br/> cfile file; <br/> try <br/>{< br/> If (! File. open (strlocalfilename, cfile: sharedenynone | cfile: moderead) <br/> return false; <br/> dwfilelength = file. getlength (); <br/> If (dwfilelength <= 0) <br/> return false; <br/> pfilebuff = new char [dwfilelength]; <br/> memset (pfilebuff, 0, sizeof (char) * dwfilelength); <br/> file. read (pfilebuff, dwfilelength); </P> <p> const int ntimeout = 5000; <br/> CIs. setoption (internet_option_connect_timeou T, ntimeout); // connection timeout setting <br/> CIs. setoption (internet_option_connect_retries, 1); // retry once <br/> Primary privilege = CIs. gethttpconnection (strserver, wport); // get an HTTP connection </P> <p> phf = ABAQUS-> openrequest (chttpconnection: http_verb_post, strobject ); <br/> If (! Phf-> sendrequest (null, 0, pfilebuff, dwfilelength) <br/>{< br/> Delete [] pfilebuff; <br/> pfilebuff = NULL; <br/> phf-> close (); <br/> SCC-> close (); <br/> CIs. close (); <br/> return false; <br/>}< br/> DWORD dwstatecode = 0; <br/> phf-> queryinfostatuscode (dwstatecode ); </P> <p> If (dwstatecode = http_status_ OK) <br/> bresult = true; <br/>}</P> <p> catch (cinternetexception * PEX) <br/>{< br/> char SZ [256] = ""; <br/> pex-> geterrormessage (SZ, 25); <br/> cstring STR; <br/> Str. Format ("internetexception occur! /R/n % s ", SZ); <br/> afxmessagebox (STR); <br/>}< br/> catch (cfileexception & Fe) <br/>{< br/> cstring STR; <br/> Str. format ("fileexception occur! /R/n % d ", Fe. m_loserror); <br/> afxmessagebox (STR); <br/>}< br/> catch (...) <br/>{< br/> DWORD dwerror = getlasterror (); <br/> cstring STR; <br/> Str. format ("unknow exception occur! /R/n % d ", dwerror); <br/> afxmessagebox (STR); <br/>}</P> <p> Delete [] pfilebuff; <br/> pfilebuff = NULL; <br/> file. close (); <br/> phf-> close (); <br/> SCC-> close (); <br/> CIs. close (); <br/> return bresult; <br/>}</P> <p>

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.