环境:
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).这问题该如何解决呢?向大家求教.先谢谢各位.

解决方案 »

  1.   

    本帖最后由 VisualEleven 于 2010-12-20 13:20:40 编辑
      

  2.   

    你的数据是不是Uncode编码的呢, 如果是那就不需要转码, 否则就.....
    content = strBuff; 这里Content是Unicode编码的, strBuff是Char类型,是ansi的, 这个语句本来就是有问题的.
      

  3.   

    数据是不是Uncode编码的呢, 如果是那就不需要转码, 否则就.....
    content = strBuff; 这里Content是Unicode编码的, strBuff是Char类型,是ansi的, 这个语句本来就是有问题的.
      

  4.   

    楼主试试这个//strUrl为网址,type为编码格式
    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;
    }
      

  5.   

    知道为什么用read会丢失了,其实不是丢失是多了一截,因为最后没有处理'\0'.现在发现不同的网站采用不同的编码方式,所以有些网站还是会出现乱码的情况,我想把所有内容保存到一个char数组里面,但是要怎么才能知道需要一个多大的char呢.以便于保存所有的数据.之后我再进行转换格式.
      

  6.   

    读取内容的时候分析返回的HTTP头,里面会有返回的内容的相关编码信息,如果不是你要的utf-8的,则转换一下就好了!