代码如下:
myurl=" http://www.csdn.net ";
CInternetSession session;
CHttpFile* theFile = NULL;
theFile = (CHttpFile*)session.OpenURL(myurl);
CString szLine;
while(theFile->ReadString(szLine))
szLine中汉字为乱码
若所要获取的网页为gb2312时可正常显示,但是csdn为utf-8编码,中文显示为乱码
<meta http-equiv="Content-Type" content="tet/html; charset=utf-8"/>
这种情况需要将UTF8字符串转换为gb2312才能正确显示中文
方法如下:
//将UTF8字符串转换为gb2312
void CLangConvert::ConvertUTF8toGB2312(CString str,_bstr_t& bstr)
{
int n=MultiByteToWideChar(CP_UTF8,0,str,str.GetLength(),NULL,0);
WCHAR * pChar = new WCHAR[n+1];
n=MultiByteToWideChar(CP_UTF8,0,str,str.GetLength(),pChar,n);
pChar[n]=0;
OLECHAR * pTempBuffer = pChar;
BSTR strPassword = SysAllocString(pTempBuffer);
_bstr_t aa (strPassword,FALSE);
bstr=aa;
SysFreeString(strPassword);
delete []pChar;
}
myurl=" http://www.csdn.net ";
CInternetSession session;
CHttpFile* theFile = NULL;
theFile = (CHttpFile*)session.OpenURL(myurl);
CString szLine;
while(theFile->ReadString(szLine))
szLine中汉字为乱码
若所要获取的网页为gb2312时可正常显示,但是csdn为utf-8编码,中文显示为乱码
<meta http-equiv="Content-Type" content="tet/html; charset=utf-8"/>
这种情况需要将UTF8字符串转换为gb2312才能正确显示中文
方法如下:
//将UTF8字符串转换为gb2312
void CLangConvert::ConvertUTF8toGB2312(CString str,_bstr_t& bstr)
{
int n=MultiByteToWideChar(CP_UTF8,0,str,str.GetLength(),NULL,0);
WCHAR * pChar = new WCHAR[n+1];
n=MultiByteToWideChar(CP_UTF8,0,str,str.GetLength(),pChar,n);
pChar[n]=0;
OLECHAR * pTempBuffer = pChar;
BSTR strPassword = SysAllocString(pTempBuffer);
_bstr_t aa (strPassword,FALSE);
bstr=aa;
SysFreeString(strPassword);
delete []pChar;
}
转载于:https://blog.51cto.com/whatday/1382614