I have migrated from Delphi 6 to Delphi 11 (64 bit edition) and in that I'm using the Indy and ZLib components. I have migrated from Indy 9 to Indy 10, using the component to post API and before that I'm writing the String to a Stream via:
var
XML: String;
stream: TStream;
begin
...
stream.WriteBuffer(XML[1], Length(XML) * 2);
And then compressing it using the ZLib component. But when it reaches the server it includes the space for every letter in the message:
< h t m l > u s e r < / h t m l >
Any idea on how to resolve this issue?
Since Delphi 2009, the
stringtype has been a UTF-16 encodedUnicodeString. You are writing the raw character bytes of astringas-is to yourTStream, hence why you need to multiply the string's length by 2 sinceSizeOf(Char)is 2 bytes. The spaces you are seeing are actually byte#$00, as ASCII characters in UTF-16 have their high byte set to zero.But in Delphi 6, the
stringtype was a 8-bitAnsiStringinstead, andSizeOf(Char)was 1 byte.To get the same behavior in Delphi 11 that you had in Delphi 6, you need to convert your
UnicodeStringcharacters into encoded bytes before writing them to yourTStream. The default/preferred encoding for XML is UTF-8 (but can be any other charset you choose to specify in the XML's prolog), eg:Alternatively, Indy has overloaded
WriteStringToStream()functions in theIdGlobalunit, which have an optionalADestEncodingparameter, eg:Alternatively, you can use Delphi's own
TStringStreamclass instead, which has an optionalTEncodingparameter in Delphi 2009+, eg:Alternatively, simply don't use a
TStreamat all. Indy'sTIdIOHandlerclass has aDefStringEncodingproperty for textual reads/writes, and itsWrite(string)andWriteLn(string)methods also have an optionalAByteEncodingparameter, eg:or: