http://blog.csdn.net/gnixuyil/article/details/7688439
获取网络图片
[cpp] view plain copy
- CString URL="http://www.google.com.hk/images/srpr/logo3w.png"
- CInternetSession session;
- CHttpFile *httpFile = (CHttpFile *)session.OpenURL(URL);
- CStdioFile imgFile;
- char buff[1024]; // 缓存
- imgFile.Open("图片名字.png", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
- DWORD dwStatusCode;
- httpFile->QueryInfoStatusCode(dwStatusCode);
- if(dwStatusCode == HTTP_STATUS_OK) {
- int size=0;
- do {
- size = httpFile->Read(buff,1024); // 读取图片
- imgFile.Write(buff,size);
- }while(size > 0);
- }
- httpFile->Close();
- session.Close();
获取URL的html
[cpp] view plain copy
- CInternetSession session;
- CHttpFile *httpFile = (CHttpFile *)session.OpenURL(m_URL);
- DWORD dwStatusCode;
- httpFile->QueryInfoStatusCode(dwStatusCode);
- CString getdata=_T("");
- if(dwStatusCode == HTTP_STATUS_OK) {
- CString line_data=_T("");
- while(httpFile->ReadString(line_data)) {
- getdata += line_data; // 读取html
- }
- getdata.TrimRight();
- }
- httpFile->Close(); // html数据已经放在getdata中
- session.Close();
- // 如果 getdata 中保存的是UTF_8网页(可以看html的meta字段)
- strCoding cfm; // 编码转换类,详情请看下方连接
- string temp = (LPCSTR)getdata.GetBuffer(); // 网页数据,转换成string型
- string output;
- // UTF_8转GB2312,让MFC控件能显示
- cfm.UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data()));
- // 若MFC字符集为Unicode的话,还需要将多字节转为宽字节
- temp = output;
- DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, NULL, 0);
- wchar_t *pwText;
- pwText = new wchar_t[dwNum];
- MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, pwText, dwNum);
- // 取得转换后结果 m_data 用于显示
- m_data = pwText;
- delete []pwText;
编码转换类:http://blog.csdn.net/gnixuyil/article/details/7688469
时间: 2024-09-30 05:38:18