views:

234

answers:

2

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"&gt; '+
          '<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 only 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?

A: 

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.

Remy Lebeau - TeamB
A: 

Thanks for the quick reply. The only reason I added the ContentType was because it wasn't working after the conversion, and I was looking at other examples that used it. Removing it doesn't help the problem though.

Dusten
This should have been posted as a comment to my earlier answer, not as a new answer.
Remy Lebeau - TeamB
In any case, if it is still not working, then either the XML is becoming corrupted, or the server cannot handle TIdHTTP's encoding correctly. Something to keep in mind is that AnsiString is codepage-aware in D2010, so it is possible that your XML is being corrupted before Indy even sees it. Since your XML is using ISO-8859-1, I suggest you use an AnsiString type that actually uses ISO-8859-1 as its codepage, ie: type Latin1String = AnsiString(28591); var XMLString: Latin1String; Worse case scenerio, you may have to use the TStream version of Post() instead of the TStrings version...
Remy Lebeau - TeamB
... and encode the post data manually. That is commonly needed when posting XML data in general.
Remy Lebeau - TeamB