I have a THTTPClient
giving a strange response (apparently UTF-16 encoding?) when invoking lHttpResp.ContentAsString()
.
The string comes through as this:
㰀㼀砀洀氀 瘀攀爀猀椀漀渀㴀∀⸀ ∀ 攀渀挀漀搀椀渀最㴀∀唀吀䘀ⴀ㘀∀ 猀琀愀渀搀愀氀漀渀攀㴀∀礀攀猀∀㼀㸀㰀刀䔀匀唀䰀吀㸀 㰀倀䄀夀倀䄀䜀䔀唀刀䰀㸀栀琀琀瀀猀㨀⼀⼀攀攀⸀琀攀猀琀⸀瀀愀礀最愀琀攀眀愀礀⸀挀漀洀⼀䠀漀猀琀倀愀礀匀攀爀瘀椀挀攀⼀瘀⼀栀漀猀琀瀀愀礀⼀瀀愀礀瀀愀最攀⼀㘀㜀 㔀㘀㘀㈀㔀㐀㌀㈀㐀欀䬀䬀儀㔀唀漀䈀椀吀氀㠀䔀爀䈀䴀 戀㰀⼀倀䄀夀倀䄀䜀䔀唀刀䰀㸀 㰀匀䔀匀匀䤀伀一吀伀䬀䔀一㸀㘀㜀 㔀㘀㘀㈀㔀㐀㌀㈀㐀欀䬀䬀儀㔀唀漀䈀椀吀氀㠀䔀爀䈀䴀 戀㰀⼀匀䔀匀匀䤀伀一吀伀䬀䔀一㸀㰀⼀刀䔀匀唀䰀吀㸀
Running Fiddler, I can see the response is fine when looking at the raw or text view, but matches the above encoding when looking at Webview. I'm probably missing something pretty obvious here, but I've tried converting with TEncoding
to no avail, as per this thread:
Delphi - converting string back from UTF-8
Fiddler's text view gives a correct text:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<RESULT>
<PAYPAGEURL>https://url/</PAYPAGEURL>
<SESSIONTOKEN>1670565241202KKv4NPBBScANOL6rxbi</SESSIONTOKEN>
</RESULT>
THTTPClient
doesn't know the data is encoded as UTF-16 and so decodes the raw bytes using the wrong encoding, giving you Mojibake. What are the values of thelHttpResp.ContentCharSet
andlHttpResp.ContentEncoding
properties? Does the HTTP response'sContent-Type
header specify UTF-16 as itscharset
? Is there a UTF-16 BOM in front of the XML? Without either of those, the client would have to actually parse the XML prolog to discover that UTF-16 is being used. I don't know ifTHTTPClient
does that. Indy'sTIdHTTP
does.x.encode('utf-16-le').decode('utf-16-be')
gives axml
string providing thatx=<your shared string>
UTF-16
without endianness is still a valid value, because the beginning<xml
can clearly be recognized as LE or BE. Also the standard has enough examples so no implementation should be able to fuxx this up, even without BOM.TIdHTTP
ignores the charset in theContent-Type
for HTML and XML payloads (and eventually JSON, too) and instead parses the data itself to discover the actual charset and endian used.