我想将使用InternetReadFile下载的utf-8编码的网页源代码转换为Windows可以识别的utf-16。

这是代码片段:

HINTERNET hConnect,hSession;
char *szBuffer = malloc(WEBSOURCE_SIZE);
wchar_t *wszBuffer = (wchar_t *)malloc(WEBSOURCE_SIZE);
TCHAR szStr[100];
DWORD dwSize=0;
DWORD dwDownloaded;
if(szBuffer == NULL)
{
    MessageBox(hwndGetBtn,TEXT("error"),TEXT("Error"),MB_ICONHAND);
}
GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
ZeroMemory(szBuffer,WEBSOURCE_SIZE);
while (InternetReadFile(hConnect,szBuffer,WEBSOURCE_SIZE,&dwDownloaded))
{
    if (0==dwDownloaded) break;
    szBuffer[dwDownloaded]=0;
}
len=MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,NULL,0);
MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,wszBuffer,sizeof(wszBuffer));

SetWindowText(hwndShowEdt,wszBuffer);


当网页以utf-8编码时,hwndShowEdt编辑框始终显示无法识别的代码。

谁能找到我代码的错误?

最佳答案

网页的字符集只能由HTML meta标签识别:

<meta http-equiv="Content-Type" content="text/html;charset=gb2312">

关于c - SetWindowText显示InternetReadFile()中无法识别的代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4914985/

10-12 20:43