CString GetHttpFileData(CString strUrl) { CInternetSession Session(_T("Internet Explorer"), 0); CHttpFile *pHttpFile = NULL; CString strData; CString strClip; pHttpFile = (CHttpFile*)Session.OpenURL(strUrl); while (pHttpFile->ReadString(strClip)) { char *pStr = (char *)strClip.GetBuffer(strClip.GetLength()); //取得str对象的原始字符串 int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, pStr, -1, NULL, 0); //取得所需缓存的多少 wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间 MultiByteToWideChar(CP_UTF8, 0, pStr, -1, pBuffer, nBufferSize*sizeof(wchar_t));//转码 //MessageBoxW(NULL, pBuffer, L"Text", MB_OK); //显示 strClip.Format(_T("%s"), pBuffer); //AfxMessageBox(strClip); free(pBuffer); //释放缓存 strData += strClip; strData += _T("\n"); } return strData; } void CWuMaDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 CString csHtml; //csHtml=UTF8ToUnicode(GetHttpFileData(_T("http://www.baidu.com"))); csHtml=GetHttpFileData(_T("http://www.taobao.com")); //AfxMessageBox(csHtml); CString pszFileName = _T("d://myfile.htm"); CFile myFile; CFileException fileException; CFile cf; if (cf.Open(pszFileName, CFile::modeCreate | CFile::modeWrite)) { int len = csHtml.GetLength(); cf.Write(csHtml.GetBuffer(len), len); cf.Close(); csHtml.ReleaseBuffer(); AfxMessageBox(_T("保存成功")); } }
时间: 2024-10-25 22:25:18