【轉】c++ http下載檔案

來源:互聯網
上載者:User

標籤:cpp   delete   query   memory   info   code   turn   class   soft   

#include <afx.h>#include <afxinet.h>#define RECVPACK_SIZE 2048bool DownloadSaveFiles(char* url,char *strSaveFile) {//下載檔案並儲存為新檔案名稱    bool ret=false;    CInternetSession Sess("lpload");    Sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT     , 2000); //2秒的連線逾時    Sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT        , 2000); //2秒的發送逾時    Sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT     , 2000); //2秒的接收逾時    Sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT   , 2000); //2秒的發送逾時    Sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 2000); //2秒的接收逾時    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) {                //查詢檔案長度                DWORD nLen=0;                cFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, nLen);                //CString strFilename = GetFileName(url,TRUE);                nBufLen=nLen;                if (nLen <= 0) break;//                //分配接收資料緩衝                pBuf = (char*)malloc(nLen+8);                ZeroMemory(pBuf,nLen+8);                char *p=pBuf;                while (nLen>0) {                    //每次下載8K                    int n = cFile->Read(p,(nLen<RECVPACK_SIZE)?nLen:RECVPACK_SIZE);                    //接收完成退出迴圈                    if (n <= 0) break;//                    //接收緩衝後移                    p+= n ;                    //剩餘長度遞減                    nLen -= n ;                }                //如果未接收完中斷退出                if (nLen != 0) break;                //接收成功儲存到檔案                CFile file(strSaveFile, CFile::modeCreate | CFile::modeWrite);                file.Write(pBuf,nBufLen);                file.Close();                ret = true;            }        } catch(...) {            break;//        }    } while(0);    //釋放緩衝    if (pBuf) {        free(pBuf);        pBuf=NULL;        nBufLen = 0 ;    }    //關閉下載串連    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

【轉】c++ http下載檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.