C++實現(xiàn)下載的代碼
更新時間:2014年10月09日 10:26:49 投稿:shichen2014
這篇文章主要介紹了C++實現(xiàn)下載的代碼,以下載百度圖片為例較為完整的講述了C++下載的具體實現(xiàn)方法,需要的朋友可以參考下
本文實例講述了C++實現(xiàn)下載的方法,分享給大家供大家參考。
具體實現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
#include <UrlMon.h>
#include <WinInet.h>
#pragma comment(lib,"wininet")
void CFileDownloadDlg::OnBnClickedBtnDownload()
{
// 使用UrlDownloadToFile函數(shù)
HRESULT hRet = URLDownloadToFile(NULL,");
if (S_OK != hRet)
{
MessageBox("下載失敗");
return;
}
//使用windows internet 庫
HINTERNET hSession = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET hLink2 = InternetOpenUrl(hSession, " if (hLink2 != NULL)
{
BYTE temp[1024];
DWORD dwNum = 1;
FILE *hFile;
if ((hFile = fopen("c:\\temp\\2.gif", "wb")) != NULL)
{
while (dwNum>0)
{
InternetReadFile(hLink2, temp, 1024, &dwNum);
fwrite(temp, sizeof(char), dwNum, hFile);
}
fclose(hFile);
MessageBox("download finished...");
}
InternetCloseHandle(hLink2);
hLink2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}
#include <WinInet.h>
#pragma comment(lib,"wininet")
void CFileDownloadDlg::OnBnClickedBtnDownload()
{
// 使用UrlDownloadToFile函數(shù)
HRESULT hRet = URLDownloadToFile(NULL,");
if (S_OK != hRet)
{
MessageBox("下載失敗");
return;
}
//使用windows internet 庫
HINTERNET hSession = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET hLink2 = InternetOpenUrl(hSession, " if (hLink2 != NULL)
{
BYTE temp[1024];
DWORD dwNum = 1;
FILE *hFile;
if ((hFile = fopen("c:\\temp\\2.gif", "wb")) != NULL)
{
while (dwNum>0)
{
InternetReadFile(hLink2, temp, 1024, &dwNum);
fwrite(temp, sizeof(char), dwNum, hFile);
}
fclose(hFile);
MessageBox("download finished...");
}
InternetCloseHandle(hLink2);
hLink2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}
希望本文所述對大家的C++程序設(shè)計有所幫助。
相關(guān)文章
詳解C/C++ Linux出錯處理函數(shù)(strerror與perror)的使用
我們知道,系統(tǒng)函數(shù)調(diào)用不能保證每次都成功,必須進行出錯處理,這樣一方面可以保證程序邏輯正常,另一方面可以迅速得到故障信息。本文主要為大家介紹兩個出錯處理函數(shù)(strerror、perror)的使用,需要的可以參考一下2023-01-01Sublime Text 3 實現(xiàn)C++代碼的編譯和運行示例
下面小編就為大家?guī)硪黄猄ublime Text 3 實現(xiàn)C++代碼的編譯和運行示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09