您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

C++ 获取URL图片、html文件,CInternetSession

时间:01-16来源:作者:点击数:

获取网络图片

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;
}

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门