#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