IdHttp Post Method Delphi 2010
Like others before me, I'm having troubles using the IdHttp(Indy 10.5.5) component in Delphi 2010. The code works fine in Delphi 7:
var
XMLString : AnsiString;
lService : AnsiString;
ResponseStream: TMemoryStream;
InputStringList : TStringList;
begin
ResponseStream := TMemoryStream.Create;
InputStringList := TStringList.Create;
XMLString :='<?xml version="1.0" encoding="ISO-8859-1"?> '+
'<!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send" "http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd"> '+
'<pnet_imessage_send> '+
' <cid>username</cid> '+
' <pw>password</pw> '+
' <vehicle_number>tr01</vehicle_number> '+
' <deliver>now</deliver> '+
' <action> '+
' <action_type>reply_with_freeform</action_type> '+
' <urgent_reply>yes</urgent_reply> '+
' </action> '+
' <freeform_message>Test Message Version 2</freeform_message> '+
'</pnet_imessage_send> ';
lService := 'imessage_send';
InputStringList.Values['service'] := lService;
InputStringList.Values['xml'] := XMLString;
try
IdHttp1.Request.Accept := '*/*';
IdHttp1.Request.ContentType := 'text/XML';
IdHTTP1.Post('http://open.peoplenetonline.com/scripts/open.dll', InputStringList, ResponseStream);
...
finally
ResponseStream.Free;
InputStringList.Free;
end;
The 开发者_如何学Pythononly differences so far between this and the D7 code is that I've changed the String types to AnsiString, and added the HTTP Request properties.
The response I get back from the server is 'XML failed to parse. Whitespace expected at Line:1 Position: 19', I'm assuming the XML got garbled up somewhere in the process, but I can't figure our where I'm going wrong.
Any ideas?
The TStrings version of Post() encodes the input data according to the 'application/x-www-form-urlencoded' content type by default, but you are setting the ContentType to 'text/xml' instead, even though you are not actually posting raw XML data by itself. If you were not setting the ContentType in your D7 code, then TIdHTTP was setting the ContentType to 'application/x-www-form-urlencoded' for you. You need to miror that same behavior in your D2010 code, either by setting the same ContentType value yourself, or by removing the assignment again so TIdHTTP can do it for you again.
精彩评论