MFC download and save file code reprint via URL

Source: Internet
Author: User
Tags save file

Http://blog.csdn.net/charlessimonyi/article/details/8666108?utm_source=tuicool&utm_medium=referral

We know that the Windows API for network connectivity is in Wininet.h, and in MFC, these APIs are encapsulated as classes.

CInternetSession class: Directly inherits from the CObject class, which is used to establish a session with an Internet server

CInternetConnection class: Helps users manage connections to Internet servers while also providing some functions to complete and respond to server communications

The CInternetConnection class also derives three classes:

CHttpConnection class: Managing Connections to HTTP Servers

CFtpConnection class: For managing connections to FTP servers

CGopherConnection class: Managing Connections to Gopher servers

We need CInternetSession, CHttpConnection, chttpfile to implement download and save files based on URLs.

What does chttpfile use to do? It can be associated with a Web object that the URL points to, because CHttpFile inherits and CFile, we can download it from the HTTP server just like a CFile file.

MFC is the CHttpFile object through the read and write to complete the dialogue with the HTTP server, including Get/post submit data, receive data.

Here is my own write a function based on the URL to download and save the file, note that you need to use the MFC project in the precompiled header file to include # include <AFXINET.H>, because the above-mentioned classes are all in this header file.

The first parameter is a URL, and the second parameter is the save path.

return value:

0: Successful Download save

-1:url not correct

-2: Failed to establish network connection

-3: Failed to initiate a GET request to the server

-4: The server does not accept requests

-5: Parameter one cannot be empty

-6: Parameter two cannot be empty

-7: Failed to create or write to file when saving file

1INT GetFile (ConstCString strURL,ConstCString Strsavepath)2 {3     //Check the two parameters passed in4     if(Strurl.isempty ())5         return-5;6     if(Strsavepath.isempty ())7         return-6;8 9Unsigned ShortNport;//used to save the destination HTTP service PortTenCString strserver, Strobject;//strserver is used to save the server address, Strobject is used to save the file object name OneDWORD Dwservicetype,dwret;//Dwservicetype is used to save the service type, Dwret is used to save the status number returned by the Submit GET request A  -     //parse URL, get information -     if(!AfxParseURL (strURL, Dwservicetype, strserver, Strobject, nport)) the     { -         //parse failed, the URL is incorrect -         return-1; -     } +     //Create a network Connection object, an HTTP connection object pointer, and a pointer to the Httpfile file object for the connection, note that the delete - CInternetSession intsess; +CHttpFile *phtfile =NULL; A     //Establish a network connection atCHttpConnection *phtcon =intsess. GetHttpConnection (strserver,nport); -     if(Phtcon = =NULL) -     { -         //failed to establish network connection - intsess. Close (); -         return-2; in     } -     //initiating a GET request toPhtfile = phtcon->openrequest (chttpconnection::http_verb_get,strobject); +     if(Phtfile = =NULL) -     { the         //failed to initiate GET request * intsess. Close (); $         DeletePhtcon;phtcon =NULL;Panax Notoginseng         return-3; -     } the     //Submit Request +Phtfile->SendRequest (); A     //Gets the status number returned by the server thePhtfile->QueryInfoStatusCode (dwret); +     if(Dwret! =HTTP_STATUS_OK) -     { $         //server does not accept requests $ intsess. Close (); -         DeletePhtcon;phtcon =NULL; -         DeletePhtfile;phtfile =NULL; the         return-4; -     }Wuyi     //Get File Size theUINT Nfilelen = (UINT) phtfile->getlength (); -DWORD Dwread =1;//used to identify how much read, 1 is to enter the loop Wu     //Creating Buffers -CHAR *szbuffer =Newchar[nfilelen+1]; About TRY $     { -         //Create a file -CFile Picfile (strsavepath,cfile::modecreate| cfile::modewrite|cfile::shareexclusive); -          while(dwread>0) A         {   +             //emptying buffers thememset (Szbuffer,0, (size_t) (nfilelen+1)); -             //read to buffer $Dwread = phtfile->Read (Szbuffer,nfilelen); the             //Write to File the Picfile.write (szbuffer,dwread); the         } the         //Close File - picfile.close (); in         //Freeing Memory the         Delete[]szbuffer; the         DeletePhtfile; About         DeletePhtcon; the         //Turn off network connections the intsess. Close (); the     } + CATCH (cfileexception,e) -     { the         //Freeing MemoryBayi         Delete[]szbuffer; the         DeletePhtfile; the         DeletePhtcon; -         //Turn off network connections - intsess. Close (); the         return-7;//Read and write file exceptions the     } the End_catch the         return 0; -}

MFC download and save file code reprint via URL

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.