//有三种方法
'方法一(只适用于MFC工程):
//参数URL:网页地址
//返回CString就是网页源码
//返回的网页源码是UTF8格式,要转成ANSI等编码才能正常显示
#include <afxinet.h>
CString GetWebSource(CString URL)
{
CInternetSession Session(NULL,0);
CString PageData;
CString TempData;
CHttpFile*HttpFile=(CHttpFile*)Session.OpenURL(URL);
while(HttpFile->ReadString(TempData))
{
PageData+=TempData;
}
HttpFile->Close();
delete HttpFile;
Session.Close();
//这里返回的网页源码是UTF8格式,要转成ANSI等编码才能正常显示
return PageData;
}
'方法二:
首先引用:
#import <msxml4.dll> named_guids
using namespace MSXML2;
CString resaa;
IXMLHTTPRequestPtr httpRes;
HRESULT hr=httpRes.CreateInstance("MSXML2.XMLHTTP");
if(!SUCCEEDED(hr))
{
AfxMessageBox("无法创建XMLHTTP对象,请检查是否安装了MS XML运行库!");
}
LPCTSTR url="http://localhost/changjun/asxml.asp";
httpRes->open("Get",url,false,"","");
httpRes->send();
if((httpRes->readyState)==4) //4时表示数据已加载完
{
resaa=httpRes->responseText.copy();
}
httpRes.Release();
//到这里,再把UTF8转成char就可以正常显示网页源码了
'方法三:
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
char* GetWebPage(char*URL)
{
HINTERNET Session = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
if (NULL == Session)
{
InternetCloseHandle(Session);
return FALSE;
}
HINTERNET hHttpFile =InternetOpenUrl(Session,URL,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL);
if (NULL == hHttpFile)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(Session);
return FALSE;
}
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize=10*1024;
if(::HttpQueryInfo(hHttpFile,5, szSizeBuffer,&dwLengthSizeBuffer,NULL)==TRUE)
{
// allocating the memory space for http file contents
dwFileSize=atol(szSizeBuffer);
}
PBYTE pBuf = new BYTE[dwFileSize*sizeof(TCHAR)];
if (NULL == pBuf)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(Session);
return FALSE;
}
DWORD dwReadDataLength = NULL;
BOOL bRet = TRUE;
do
{
//ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR));
bRet = InternetReadFile(hHttpFile,pBuf,dwFileSize,&dwReadDataLength);
} while (NULL != dwReadDataLength);
return (char*)pBuf;
}