CWebWorld
/*=================CWebWorld.h 头文件=======================*/ #include "wininet.h" class CWebWorld { public: void SetErrorMessage(CString s); CString GetWebPage(const CString& Url); CWebWorld(); virtual ~CWebWorld(); private: CString m_ErrorMessage; HINTERNET m_Session; }; /*=================CWebWorld.h 头文件=======================*/ /* //------------------------------------------------------------------------------------------------------------------ // WebWorld.cpp: implementation of the CWebWorld class. //------------------------------------------------------------------------------------------------------------------ */ #include "stdafx.h" #include "WebWorld.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #define AGENT_NAME "QBCardQuery1.0" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// #pragma comment(lib,"Wininet.lib") CWebWorld::CWebWorld() { DWORD dwError; // Initialize the Win32 Internet functions m_Session = ::InternetOpen(AGENT_NAME, INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings. NULL, // Proxy name. NULL indicates use default. NULL, // List of local servers. NULL indicates default. 0) ; dwError = GetLastError(); } CWebWorld::~CWebWorld() { // Closing the session ::InternetCloseHandle(m_Session); } CString CWebWorld::GetWebPage(const CString& Url) { // DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); CString Contents; // Setting default error message Contents = m_ErrorMessage; CString strServer,strPath; if(Url.IsEmpty()) { return m_ErrorMessage; } //去掉"http://" CString strTemp=Url.Mid(7); //检查主机的路径 int nSlash=strTemp.Find("/"); if (nSlash!=-1) //如等于-1,就是没找到 { strServer=strTemp.Left(nSlash);//取‘/’左边的服务器地址 strPath=strTemp.Mid(nSlash); } else { strServer=strTemp; } m_Session=::InternetOpen("AngelStar Studio",PRE_CONFIG_INTERNET_ACCESS, NULL,NULL,INTERNET_INVALID_PORT_NUMBER); //判断会话句柄是否有效 if(m_Session==NULL) { AfxMessageBox("Internet session initalization"); return m_ErrorMessage; } //第二步:初始化HTTP session HINTERNET hConnect=::InternetConnect(m_Session,//当前internet会话句柄 strServer,//server name INTERNET_INVALID_PORT_NUMBER, NULL,//++++66666666666666666666"",//user name "",//password INTERNET_SERVICE_HTTP,//Type of service to access 0, 0); //判断连接句柄是否有效 if(hConnect==NULL) { AfxMessageBox("Internet connect initalization failed!"); //关闭会话句柄 VERIFY(::InternetCloseHandle(m_Session)); return m_ErrorMessage; } //第三步:打开一个HTTP请求句柄 HINTERNET hHttpFile=::HttpOpenRequest(hConnect, "GET", strPath, HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); //判断连接句柄是否有效 //判断会话句柄是否有效 if(hHttpFile==NULL) { AfxMessageBox("Http request failed!"); VERIFY(::InternetCloseHandle(hConnect)); VERIFY(::InternetCloseHandle(m_Session)); return m_ErrorMessage; } //显示等待光标 CWaitCursor wait; //第四步:发出请求 BOOL bSendRequest=::HttpSendRequest(hHttpFile, NULL, 0, 0, 0); if(bSendRequest) { //得到文件的大小 char achQueryBuf[16]; DWORD dwFileSize; DWORD dwQueryBufLen=sizeof(achQueryBuf); BOOL bQuery=::HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH, achQueryBuf, &dwQueryBufLen, NULL); if(bQuery) { //查找成功,指出需要存放文件的内存大小??????? dwFileSize=(DWORD)atol(achQueryBuf); } else { //失败,猜出一个最大文件数 dwFileSize=100*1024; } //分配一个缓冲区给文件数据 char *lpszBuf=new char[dwFileSize+1]; ZeroMemory(lpszBuf,dwFileSize); //读文件 DWORD dwBytesRead; // BOOL bRead=::InternetReadFile(hHttpFile, // lpszBuf, // dwFileSize+1, // &dwBytesRead); char tmp[10*1024]; while (::InternetReadFile(hHttpFile,tmp,sizeof(tmp)-1,&dwBytesRead)) { if (0==dwBytesRead) break; tmp[dwBytesRead]=0;//此处非常重要,缺了这一句可能抓的内容就不正确 lstrcat(lpszBuf,tmp); ZeroMemory(tmp,sizeof(tmp)); } //显示HTML的源码 Contents = lpszBuf; //清除缓冲区 delete lpszBuf; // 关闭INTERNET句柄 VERIFY(::InternetCloseHandle(hHttpFile)); VERIFY(::InternetCloseHandle(hConnect)); VERIFY(::InternetCloseHandle(m_Session)); DWORD dwStart = GetTickCount(); DWORD dwEnd = dwStart; do { MSG msg; PeekMessage(&msg,NULL,0,0,PM_REMOVE); TranslateMessage(&msg); DispatchMessage(&msg); dwEnd = GetTickCount()-dwStart; }while(dwEnd <200); } return Contents; } void CWebWorld::SetErrorMessage(CString s) { m_ErrorMessage = s; }