Lena wrote:I saved all *.php file in UTF-8.
The encoding of the PHP itself is irrelevant. What is important is the encoding of the data it sends back to you.
Lena wrote:What is the correct way to see Russian letters?
Your code is reading the response into a TStringStream that is constructed without a TEncoding specified, so it will use TEncoding::Default, which is ANSI on Windows and UTF-8 on other platforms. If the response uses an encoding that does not match the stream's TEncoding object, the DataString property getter will run into conversion issues with non-ASCII characters. For that reason, I *NEVER* recommend reading an HTTP response into a TStringStream, because then you lose all control of charset handling.
You need to pay attention to the charset that is reported in the response and then decode the response data accordingly.
Had you used Indy''s TIdHTTP instead of Embarcadero's THTTPClient, it would have done the charset handling for you, eg:
- Code: Select all
std::unique_ptr<TIdHTTP> aHTTP(new TIdHTTP());
...
String rezult = aHTTP->Post(postlink, paramlist.get());
With THTTPClient, try something more like this:
- Code: Select all
std::unique_ptr<THTTPClient> aHTTP(THTTPClient::Create());
...
String rezult = aHTTP->Post(postlink, paramlist.get())->ContentAsString();
Or:
- Code: Select all
std::unique_ptr<THTTPClient> aHTTP(THTTPClient::Create());
...
_di_IHTTPResponse Response = aHTTP->Post(postlink, paramlist.get());
std::unique_ptr<TEncoding> Encoding(TEncoding::GetEncoding(Response->ContentCharSet));
String rezult = Response->ContentAsString(Encoding.get());
Lena wrote:It is necessary to save file *.php in UTF-8 and without BOM!.
Or, simply ignore the BOM if it is present:
- Code: Select all
...
String rezult = ...
if (!result.IsEmpty())
{
if (rezult[1] == 0xFEFF)
result.Delete(1, 1);
...
}
Or, just let ParseJSONValue() handle the BOM for you:
- Code: Select all
std::unique_ptr<TBytesStream> Response(new TBytesStream());
...
aHTTP->Post(postlink, paramlist.get(), Response.get());
if (Response->Size > 0)
{
std::unique_ptr<TJSONValue> LJSONValue(TJSONObject::ParseJSONValue(Response->Bytes, 0, true));
...
}