eric3501
eric3501

Reputation: 11

TIdTCPClient.IOHandler.Write(TStream) cannot send Big5?

Through TCPClient.IOHandler.Write(StmMsg);, the message is delivered to the frontend. English is ok, but Big5 cannot be delivered, why!!??

(StmMsg: TStringStream, the program has added... TCPClient.IOHandler.DefStringEncoding:= IndyTextEncoding_UTF8;)

The following is the code:

if not TCPClient.Connected then
TCPClient.Connect;
deviceToken :=
'6aa5bfcfe731ab29b260fab38a43f1e1abac0de3d6e8e0bc5f4b89c422938e8f';
MensajeEnviar := edtMensaje.Text;

strMessage := Get_Msg(deviceToken, Get_PayLoad(MensajeEnviar, 1,
'default'));

StmMsg := TStringStream.Create(strMessage);

StmMsg.Seek(0, soBeginning);
TCPClient.IOHandler.Write(StmMsg);

Upvotes: 0

Views: 390

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597205

Big5 is not a language. It is a byte encoding used for Chinese.

The TIdIOHandler.DefStringEncoding property applies only to string operations, not to stream operations. The TIdIOHandler.Write(TStream) method writes the content of a stream as-is. So, it is your responsibility to make sure the contents of the stream are encoded properly beforehand.

However, the TStringStream constructor you are calling uses TEncoding.Default for the stream's byte encoding. On Windows1, TEncoding.Default represents the default ANSI charset of the user that is running your program. An ANSI charset will not work for Chinese text, and will lose data.

1: on non-Windows platforms, TEncoding.Default uses UTF-8 instead.

You need to use TEncoding.UTF8 instead for the stream's byte encoding, eg:

StmMsg := TStringStream.Create(strMessage, TEncoding.UTF8);

Alternatively, you can remove the stream altogether and just use the TIdIOHandler.Write(String) method instead, which will then use the TIdIOHandler.DefStringEncoding property, eg:

TCPClient.IOHandler.Write(strMessage);

Upvotes: 1

Related Questions