环境:
xp,VS2005之VC.
UpdateData(true); //从控件到成员变量
//GetSourceHtml(m_url);
CInternetSession httpSession;
CInternetFile* htmlFile = (CInternetFile*) httpSession.OpenURL(m_url);
CString content;
//while (htmlFile->ReadString(content))
char strBuff[1025] = {0}; while (htmlFile->Read((void*)strBuff, 1024))
{
content = strBuff;
m_html += content + L" ";
}
htmlFile->Close();
httpSession.Close();
UpdateData(false);像现在使用read()读取数据,就会出现最后的一部分数据丢失,就是读取完若干个1024字节后,最后不足1024长度的数据就丢失了,而如果我使用ReadString()读取数据,读取出来的数据是无法转换的乱码但数据是完整的.因为VS2005使用的是unicode编码(程序必须要使用UNICODE).这问题该如何解决呢?向大家求教.先谢谢各位.
xp,VS2005之VC.
UpdateData(true); //从控件到成员变量
//GetSourceHtml(m_url);
CInternetSession httpSession;
CInternetFile* htmlFile = (CInternetFile*) httpSession.OpenURL(m_url);
CString content;
//while (htmlFile->ReadString(content))
char strBuff[1025] = {0}; while (htmlFile->Read((void*)strBuff, 1024))
{
content = strBuff;
m_html += content + L" ";
}
htmlFile->Close();
httpSession.Close();
UpdateData(false);像现在使用read()读取数据,就会出现最后的一部分数据丢失,就是读取完若干个1024字节后,最后不足1024长度的数据就丢失了,而如果我使用ReadString()读取数据,读取出来的数据是无法转换的乱码但数据是完整的.因为VS2005使用的是unicode编码(程序必须要使用UNICODE).这问题该如何解决呢?向大家求教.先谢谢各位.
content = strBuff; 这里Content是Unicode编码的, strBuff是Char类型,是ansi的, 这个语句本来就是有问题的.
content = strBuff; 这里Content是Unicode编码的, strBuff是Char类型,是ansi的, 这个语句本来就是有问题的.
string DownHtmlContent(const string &strUrl, Html_Type type)
{
string strContent;
string strRooK = "RookIE/1.0";
string strUrlTmp = strUrl;
HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, strUrl.c_str(), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
char *Temp = new char[1024*1024];
ZeroMemory(Temp, 1024*1024);
DWORD Number = 1;
DWORD Total = 0;
BOOL bRes = FALSE;
while ( Number > 0 )
{
if( InternetReadFile(handle2, Temp+Total, 2048, &Number) )
{
Total += Number;
}
}
if ( type == CODE_GB2312 )
{
strContent = Temp;
}
else if ( type == CODE_UTF8 )
{
wchar_t *pUnicode = new wchar_t[1024*1024];
char *pAnsi = new char[1024*1024];
ZeroMemory( pAnsi, 1024*1024 );
ZeroMemory( pUnicode, 1024*1024*2 );
if( MultiByteToWideChar( CP_UTF8, 0, Temp, Total, pUnicode, 1024*1024 ) != 0 )
{
WideCharToMultiByte( CP_ACP, 0, pUnicode, wcslen( pUnicode ), pAnsi, 1024*1024, "", NULL );
strContent = pAnsi;
}
delete []pAnsi;
delete []pUnicode;
}
delete []Temp;
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
} return strContent;
}