标签:style blog http color os io for ar
最近又在网上找了一些有关MFC抓取网页代码的文章看,发现有个比较简单的代码,和大家分享下。
CInternetSession session(NULL, 0); CHttpFile* htmlFile = NULL; CString strLine, strHtml; CString url = _T("http://www.tqyb.com.cn/data/gzWeather/gz_weatherForecastInDays.js?"); TCHAR sRecv[1024]; UINT CodePage = 65001;//CP_UTF8:65001 CP_ACP:0 strHtml = _T(""); //获取网页源码 htmlFile = (CHttpFile*)session.OpenURL(url);//重新打开连接 DWORD dwStatusCode; //接受请求返回值 htmlFile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { while (htmlFile->ReadString(sRecv, 1024)) { // 编码转换,可解决中文乱码问题 //gb2312转为unicode,则用CP_ACP //gbk转为unicode,也用CP_ACP //utf-8转为unicode,则用CP_UTF8 int nBufferSize = MultiByteToWideChar(CodePage, 0, (LPCSTR)sRecv, -1, NULL, 0); wchar_t *pBuffer = new wchar_t[nBufferSize + 1]; memset(pBuffer, 0, (nBufferSize + 1)*sizeof(wchar_t)); //gb2312转为unicode,则用CP_ACP //gbk转为unicode,也用CP_ACP //utf-8转为unicode,则用CP_UTF8 MultiByteToWideChar(CodePage, 0, (LPCSTR)sRecv, -1, pBuffer, nBufferSize*sizeof(wchar_t)); strHtml += pBuffer; strHtml += "\r\n"; delete pBuffer; } } htmlFile->Close(); session.Close(); delete htmlFile;
注意了,这个网址代码格式是UTF8,中文会乱码,所以加了一个字符之间的转换,//PageCode = 65001.
而且在设置vs时,字符集要设置成多字符。
项目->属性->字符集->多字节字符。
标签:style blog http color os io for ar
原文地址:http://www.cnblogs.com/learning-lzj2014/p/3917119.html