C++ 獲取URL圖片、html檔案,CInternetSession

喻大帥發表於2016-08-31
獲取網路圖片
CString URL="http://192.168.0.23:8080/3DView/CR201505060107001.jpg"  
CInternetSession session;  
CHttpFile *httpFile = (CHttpFile *)session.OpenURL(URL);  
CStdioFile imgFile;  
char buff[1024];    // 快取  
imgFile.Open("圖片名字.png", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);  
DWORD dwStatusCode;  
httpFile->QueryInfoStatusCode(dwStatusCode);  
if(dwStatusCode == HTTP_STATUS_OK) {  
	int size=0;  
	do {  
		size = httpFile->Read(buff,1024);    // 讀取圖片  
		imgFile.Write(buff,size);  
	}while(size > 0);  
}  
httpFile->Close();  
session.Close(); 

獲取URL的html

CInternetSession session;  
CHttpFile *httpFile = (CHttpFile *)session.OpenURL(m_URL);  
DWORD dwStatusCode;  
httpFile->QueryInfoStatusCode(dwStatusCode);  
CString getdata=_T("");  
if(dwStatusCode == HTTP_STATUS_OK) {  
	CString line_data=_T("");  
	while(httpFile->ReadString(line_data)) {   
		getdata += line_data;          // 讀取html  
	}  
	getdata.TrimRight();  
}  
httpFile->Close();   // html資料已經放在getdata中  
session.Close();  
// 如果 getdata 中儲存的是UTF_8網頁(可以看html的meta欄位)  
strCoding cfm;  // 編碼轉換類,詳情請看下方連線  
string temp = (LPCSTR)getdata.GetBuffer();  // 網頁資料,轉換成string型  
string output;  
// UTF_8轉GB2312,讓MFC控制元件能顯示  
cfm.UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data()));  
// 若MFC字符集為Unicode的話,還需要將多位元組轉為寬位元組  
temp = output;  
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, NULL, 0);  
wchar_t *pwText;  
pwText = new wchar_t[dwNum];  
MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, pwText, dwNum);  
// 取得轉換後結果 m_data 用於顯示  
m_data = pwText;  
delete []pwText; 

DEMO:

BOOL IHttpFileDownLoad::EnHttpImageDownLoad(CString strHttpUrl, CString strDownPath, CString &strDestFilePath)
{
	BOOL bFlag = FALSE;
	strHttpUrl.TrimLeft();
	strHttpUrl.TrimRight();
	if(strHttpUrl.IsEmpty() || strDownPath.IsEmpty()) return bFlag;
	if(!::PathFileExists(strDownPath)) return bFlag;
	
	CString URL = strHttpUrl;
	CInternetSession session; 
	CHttpFile *httpFile = NULL;
	try
	{
		httpFile = (CHttpFile *)session.OpenURL(URL);
	}
	catch (CInternetException* e)
	{
		e->Delete();
		return bFlag;
	}
	if(httpFile==NULL){
		session.Close();
		return bFlag;
	}
	
	CString httpFileName = httpFile->GetFileName();
	CString strFilePath = strDownPath;
	if(strFilePath.Right(1)!="\\") strFilePath+="\\";
	strFilePath += httpFileName;
	CFileFind find;
	BOOL bFind = find.FindFile(strFilePath);
	if(bFind) CFile::Remove(strFilePath);
	
	CStdioFile imgFile; 
	if (imgFile.Open(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
	{
		DWORD dwStatusCode;  
		httpFile->QueryInfoStatusCode(dwStatusCode);  
		if(dwStatusCode == HTTP_STATUS_OK) {  
			bFlag = TRUE;
			DWORD fileLength = httpFile->GetLength();
			char *buff = new char[fileLength];// 快取
			memset(buff, 0, fileLength);
			int size=0;  
			do {  
				size = httpFile->Read(buff,1024);//讀取圖片  
				imgFile.Write(buff,size);  
			}while(size > 0);  
			if (buff!=NULL)
			{
				delete [] buff;
				buff = NULL;
			}
			strDestFilePath = strFilePath;
		}  
	}
	
	imgFile.Close();
	httpFile->Close(); 
	delete []httpFile;
	session.Close();  
	return bFlag;
}


相關文章