0

I notice I have invalid characters for XML files in an application who use Indy Client (I actually use default parameters for IdHttp)

Here is my code :

  ts := TStringList.Create;
  try
    ts.Add('XML=' + AXMLDoc.XML.Text));
    HTTPString := IdHTTPClient.Post('http://' + FHost + ':' + IntToStr(FPort) + FHttpRoot, ts);
  finally
    ts.Free;
  end;

My XML file is UTF-8 encoded.

What I have to do get good encoding on my server (I also use Indy for server) ?

Hugues Van Landeghem
  • 6,755
  • 3
  • 34
  • 59

1 Answers1

5

UTF-8 is the default charset that TIdHTTP uses for submitting a TStringList object. The real issue is that XML should not be submitted using a TStringList to begin with, even with a proper charset. The reason is because the TIdHTTP.Post(TStrings) method implements the application/x-www-form-urlencoded content type, and thus url-encodes the TStringList content, which can break XML if the receiver is not expecting that. So unless the receiver is actually expecting a real application/x-www-form-urlencoded encoded request, XML should be transmitted using the TIdHTTP.Post(TStream) method instead so the raw XML bytes are preserved as-is.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It now works well on Windows :) It compile for iOS but when I try to execute on iOS, it make HTTP 500 Internal Server Error : what can be the error ? – Hugues Van Landeghem May 17 '13 at 17:15
  • There is no way to know without comparing the actual HTTP request data from both platforms. How are you creating and submitting the XML exactly? – Remy Lebeau May 17 '13 at 21:33
  • XML is an TXMLDocument and I make like this http://pastebin.com/PXq4vC1Z It's a component and it is used for Win32 and iOS – Hugues Van Landeghem May 18 '13 at 20:04
  • `TStringStream` uses `SysUtils.TEncoding`, so it also can corrupt the XML if you don't use the correct encoding. XML defaults to UTF-8 so try specifying `TEncoding.UTF8` in the `TStringStream` constructor. – Remy Lebeau May 18 '13 at 20:16
  • Again, you need to get a capture of the raw HTTP request data being transmitted by both platforms and compare them for any differences. Either a required parameter is missing, or the XML is encoded differently, or the like. A 500 reply generally means the server encountered a critical error, usually due to bad input. – Remy Lebeau May 19 '13 at 05:48