Overview
Features
Download
Documentation
Community
Add-Ons & Services

Writing Unicode to HTTPResponse

Please post support and help requests here.

Writing Unicode to HTTPResponse

Postby slavo5150 » 18 Jul 2009, 14:56

How can I write UTF-16 (or UTF-8) encoded text to the HTTPResponse.send() output stream?

I've tried this:
Code: Select all
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");

std::ostream& ostr = response.send();

ostr << L"<html><head><title>POCO HTTPServer Test</title></head>";
ostr << L"<body><p style=\"text-align: center; font-size: 48px;\">";
ostr << L"Hello in UTF16";
ostr << L"</p></body></html>";


I'm running on Windows XP which I know encodes strings as UCS2/UTF-16, so I've set the encoding of my browser to UTF-16. The output I get in IE and Firefox is:
004178E0004178580041783000417800

I've also tried using an OutputStreamConverter without success.

I eventually would like to output UTF-8, but I thought since I'm running Windows, UTF-16 would be a logical next step.

Thanks for any help,
Mike
slavo5150
 
Posts: 4
Joined: 18 Jul 2009, 14:41

Re: Writing Unicode to HTTPResponse

Postby slavo5150 » 25 Jul 2009, 16:00

I've been able to achieve my desired output using the OutputStreamConverter, and the wstring class. However, In the code below, why does "Example 1" work, but "Example 2" not work?

Code: Select all
void RequestHandler::handleRequest(
   HTTPServerRequest& request,
   HTTPServerResponse& response)
{
   response.setChunkedTransferEncoding(true);
   response.setContentType("text/html");

   UTF16Encoding utf16;
   UTF8Encoding utf8;
   OutputStreamConverter ostr(response.send(), utf16, utf8);

   //Example 1:  This works :|
   std::wstring s(L"Hello in UTF-8");
   ostr.write((char*)s.c_str(), s.length() * sizeof(wchar_t));
   
   //Example 2:  This doesn't work :(
   ostr << L"Hello in UTF-8";
}


Environment:
Windows XP, Visual Studio Express 2008
slavo5150
 
Posts: 4
Joined: 18 Jul 2009, 14:41

Re: Writing Unicode to HTTPResponse

Postby Protogenes » 30 Sep 2009, 16:06

because there is no specialized operator << (const wchar_t * Str)
pushing a wchar_t* into a stream with << interprets the data as simple void* whose hex address got printed to the stream.
Protogenes
 
Posts: 3
Joined: 29 Sep 2009, 15:47


Return to Support

Who is online

Users browsing this forum: No registered users and 5 guests

cron