How to Use cinternetsession to obtain Chinese garbled characters on the UTF-8 page

Source: Internet
Author: User

The Code is as follows:
Myurl = "http://www.csdn.net ";
Cinternetsession session;
Chttpfile * thefile = NULL;
Thefile = (chttpfile *) Session. Openurl (myurl );
Cstring szline;
While (thefile-> readstring (szline ))
Chinese characters in szline are garbled

If the webpage to be obtained is gb2312, it can be normally displayed, but csdn is UTF-8 encoded and Chinese is garbled
<Meta http-equiv = "Content-Type" content = "Tet/html; charset = UTF-8"/>

In this case, you need to convert the utf8 string to gb2312 to display Chinese characters correctly.

The method is as follows:

// Convert the utf8 string to 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.