在MFC下实现GET网页及服务器信息

/********************************************************
 *本函数在MFC下实现GET网页信息,并保存到本地文件
 *说明:
 *strURL:请求网页的URL
 *fileaddr:保存到本地的文件地址
 *返回:若成功返回0,否则返回非零值
 *******************************************************/
#include <afxinet.h>

int HTTP_GET_DATA(CString strURL, CString fileaddr)
{
	CInternetSession session;
	CHttpConnection* pHttpConnection = NULL;
	CHttpFile *pHttpFile = NULL;
	CString strServer, strObject;
	INTERNET_PORT wPort;
	DWORD dwType;
 
	if(!AfxParseURL(strURL, dwType, strServer, strObject, wPort))
	{
		return 1;//URL解析错误
	}
	pHttpConnection = session.GetHttpConnection(strServer, wPort);
	pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
	pHttpFile->SendRequest();
	DWORD dwRet;
	pHttpFile->QueryInfoStatusCode(dwRet);
	if(dwRet == HTTP_STATUS_OK)
	{
		CFile file;
		file.Open(fileaddr, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone, 0);

		char *pszBuffer = new char[1024];
		UINT nRead = pHttpFile->Read(pszBuffer, 1024); 
		while (nRead > 0)
		{
			file.Write(pszBuffer, nRead);
			nRead = pHttpFile->Read(pszBuffer, 1024);
		}
		file.Close();
		delete []pszBuffer;
	}
	if(pHttpFile != NULL)
	{
		pHttpFile->Close();
		delete pHttpFile;
		pHttpFile = 0;
	}
	if(pHttpConnection != NULL)
	{
		pHttpConnection->Close();
		delete pHttpConnection;
		pHttpConnection = 0;
	} 
	session.Close();
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值