Use Wininet to download the specified URL

zhaozj2021-02-16  56

CString CDOWNFODLG :: DownURL (CSTRING STRURL) {CinternetSession M_Session ("DIGITALTITITAN"); ChttpFile * Pfile = NULL; CEXCEPTION * E;

TCHAR szTempPath [MAX_PATH], szTempFile [MAX_PATH]; DWORD dwResult = :: GetTempPath (MAX_PATH, szTempPath); CString strURLPath; GetTempFileName (szTempPath, _T ( "DigitalTitan _"), 0, szTempFile); strURLPath = szTempFile; TRY {pFile = (Chttpfile *) m_session.openurl (Strurl);} catch_all (e) {pfile = null; afxMessageBox ("URL address is not legal", MB_ICONIONFORMATION); RETURN "";} end_catch_all

if (pFile) {DWORD dwStatus; DWORD dwBufLen = sizeof (dwStatus); BOOL bSuccess = pFile-> QueryInfo (HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, & dwStatus, & dwBufLen); if (bSuccess && dwStatus> = 200 && dwStatus <300) {CStdioFile m_File; if (m_File. Open (StrurlPath, CFile :: Modread | CFile :: Typebinary)) {Byte Pbuf [1024]; DWORD DWREAD; Do {dwread = Pfile-> Read (PBUF, 1024); M_File.Write (PBUF, DWREAD);} while (dwread> 0); m_file.close ();}} Pfile-> close (); delete pfile;} else {m_session.close ();} return strurlpath;}

转载请注明原文地址:https://www.9cbs.com/read-17560.html

New Post(0)