procedure Texport_plan.cxB_LoadClick(Sender: TObject);
var
HTTP: TIdHTTP;
Query: String;
Buffer: TMemoryStream;
loadData: Sting;
responseData: String;
begin
try
HTTP := TIdHTTP.Create(nil);
HTTP.Request.ContentEncoding := 'utf-8';
HTTP.Request.ContentType := 'text/xml';
Application.ProcessMessages;
Query := 'http://priem.edu.ru:8000/import/ImportService.svc/import';
loadData := '<Root></Root>'
Buffer := TMemoryStream.Create;
loadData.SaveToStream(Buffer);
responseData := HTTP.Post(Query, Buffer);
except
on E: EIdHTTPProtocolException do
ShowMessage(E.Message);
end;
end;
This returns HTTP/1.1 400 Bad Request. If use get from http://priem.edu.ru:8000/import/ImportService.svc/test/import i get right xml document in response. Why could this happen? I checked all i can find in google...but nothing helps.
Im using RAD Delphi XE3
UPD: Test client works fine...C# + webclient class. May be something more exists for delphi...not only IdHTTP?
I found error. If content type is */xml IdHTTP changes Charset to 'us-ansi'... To make this component works properly you need to change
to
So IdHTTP cant change Charset to us-ansi and you will get encoding you want.
Edit
It turned out that the assignment order of the
Charset
andContentType
is important.If you use a
ContentType
containing thexml
,xml-external-parsed-entity
words or ending with+xml
, the Charset is forcibly set to the value'us-ascii'
and to'ISO-8859-1'
in all other cases.The behavior is undocumented, so you're free to think it is a bug or an implementation detail.
Comments from Remy in the body of
TIdEntityHeaderInfo.SetContentType
(IdHTTPHeaderInfo.pas
) say:To change the charset, you must set it after setting the
ContentType
value, like this:Original answer
(still valid)
You're indicating your content is encoded in
utf-8
, but Delphi strings areutf-16
encoded by default.I have no Delphi at hand to check if the string helper have a overloaded version of the
SaveToStream
method which supports encodings, but I bet it have, so you can try:before sending your data.