编写的网页源码读取函数出错解决方案
编写的网页源码读取函数出错
函数是:
void getURLContext(CString strURL)
{
CInternetSession session;
CHttpFile *pFile=NULL;
CString strContext;
pFile=(CHttpFile*)session.OpenURL(strURL);
CString strLine;
CString CurPath;
char CurPath1[MAX_PATH];
GetCurrentDirectory(MAX_PATH,(LPWSTR)CurPath1); //find the absolute path.
CurPath = (LPTSTR)(LPCTSTR)CurPath1;
ofstream ICG3(CurPath + _T("\\webpage.html"), ios::out);
if(pFile!=NULL)
{
while(pFile->ReadString(strLine)!=NULL)
{
strContext += strLine;
ICG3<<strLine;
}
}
AfxMessageBox(strContext);
session.Close();
pFile->Close();
delete pFile;
}
AfxMessageBox(strContext)能够正常显示源码,但是webpage.html不能,里面都是00DB121000DB121000DB1210 一些这样的符号。请问如何读取网页源码到文件中去。
3X。
------解决方案--------------------
那种方法我也没试过。要不,你换这种方法,我测试过的
函数是:
void getURLContext(CString strURL)
{
CInternetSession session;
CHttpFile *pFile=NULL;
CString strContext;
pFile=(CHttpFile*)session.OpenURL(strURL);
CString strLine;
CString CurPath;
char CurPath1[MAX_PATH];
GetCurrentDirectory(MAX_PATH,(LPWSTR)CurPath1); //find the absolute path.
CurPath = (LPTSTR)(LPCTSTR)CurPath1;
ofstream ICG3(CurPath + _T("\\webpage.html"), ios::out);
if(pFile!=NULL)
{
while(pFile->ReadString(strLine)!=NULL)
{
strContext += strLine;
ICG3<<strLine;
}
}
AfxMessageBox(strContext);
session.Close();
pFile->Close();
delete pFile;
}
AfxMessageBox(strContext)能够正常显示源码,但是webpage.html不能,里面都是00DB121000DB121000DB1210 一些这样的符号。请问如何读取网页源码到文件中去。
3X。
------解决方案--------------------
那种方法我也没试过。要不,你换这种方法,我测试过的
- C/C++ code
#include<windows.h> #include<wininet.h> #include<iostream> #include <tchar.h> using namespace std; #pragma comment(lib,"wininet.lib") void main() { DWORD byteread=0; char buffer[100]; memset(buffer,0,100); HINTERNET internetopen; internetopen = InternetOpen(/*_T("DownLoadFile")*/NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0); if (internetopen==NULL) { cout<<"Internet open failed!"<<endl; return; } HINTERNET internetopenurl; internetopenurl = InternetOpenUrl(internetopen,_T("http://topic.****.net/u/20090429/10/6a38f59f-b776-4140-a11b-f59c4a979931.html"), NULL,0,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_PRAGMA_NOCACHE,0); //下载的URL if (internetopenurl==NULL) { cout<<"Internet open url failed! error code = "<<GetLastError()<<endl; goto there; } BOOL hwrite; DWORD written; HANDLE createfile; createfile = CreateFile(_T("c:\\downloaded.html"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); //保存文件 if (createfile==INVALID_HANDLE_VALUE) { cout<<"Create File failed!"<<endl; goto next; } BOOL internetreadfile; while(1) { internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread); if(byteread==0) break; hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL); if (hwrite==0) { cout<<"Write to file failed!"<<endl; goto here; } } cout<<"Finished downloading!"<<endl; here: CloseHandle(createfile); next: InternetCloseHandle(internetopenurl); there: InternetCloseHandle(internetopen); }