"Go" C + + http download file

Source: Internet
Author: User

#include <afx.h> #include <afxinet.h> #define Recvpack_size 2048bool downloadsavefiles (char* Url,char *    Strsavefile) {//download file and save as new file name bool Ret=false;    CInternetSession sess ("Lpload"); Sess.setoption (Internet_option_connect_timeout, 2000); 2 Seconds of Connection timeout sess.setoption (Internet_option_send_timeout, 2000); 2-Second Send timeout sess.setoption (internet_option_receive_timeout, 2000); 2-second receive timeout sess.setoption (Internet_option_data_send_timeout, 2000); 2-Second Send timeout sess.setoption (internet_option_data_receive_timeout, 2000); 2-second receive timeout DWORD Dwflag = internet_flag_transfer_binary| internet_flag_dont_cache|    Internet_flag_reload;    chttpfile* cFile = NULL;    char *pbuf = NULL;    int nbuflen = 0;            do {try{cFile = (chttpfile*) sess.openurl (Url,1,dwflag);            DWORD Dwstatuscode;            Cfile->queryinfostatuscode (Dwstatuscode);              if (Dwstatuscode = = HTTP_STATUS_OK) {//query file length  DWORD nlen=0;                Cfile->queryinfo (Http_query_content_length, Nlen);                CString strFileName = GetFileName (url,true);                Nbuflen=nlen;                if (nlen <= 0) break;////Allocate receive data cache PBuf = (char*) malloc (nlen+8);                ZeroMemory (PBUF,NLEN+8);                Char *p=pbuf; while (nlen>0) {//per download 8K int n = cfile->read (p, (nlen<recvpack_size)? nlen:r                    Ecvpack_size);                    Receive complete exit loop if (n <= 0) break;////Receive cache after move p+= N;                The remaining length is decremented by nlen-= n;                }//If you have not received the interrupt exit if (Nlen! = 0) break; Received successfully saved to file CFile files (strsavefile, Cfile::modecreate |                Cfile::modewrite); File.                Write (Pbuf,nbuflen); File.                Close ();            ret = true;       } } catch (...) {break;//}}    while (0);        Release Cache if (PBuf) {free (PBUF);        Pbuf=null;    Nbuflen = 0;        }//Close download connection if (cFile) {cfile->close ();        Sess.close ();    Delete CFile; } return ret;}    int main () {downloadsavefiles ("Http://www.nirsoft.net/utils/nircmd.zip", "D:\\cppdld_nircmd.zip"); return 0;}

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

"Go" C + + http download file

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.