views:

1602

answers:

2

I have problem with Indy IdHttp Post method. Function CallRpc() compiled with Delphi 2007 works fine but same code compiled with Delphi 2010 raises exception.

What do I have to consider when I change Delphi 2007 Indy TIdHttp to Delphi 2010 Indy TIdHttp?

function CallRpc(const sURL, sXML: string): string;
var
  SendStream : TStream;
  IdHttp     : TIdHttp;
begin
  SendStream := TMemoryStream.Create;
  IdHttp := TIdHttp.Create(nil);
  try
      IdHttp.Request.Accept        := '*/*';
      IdHttp.Request.ContentType   := 'text/sXML';
      IdHttp.Request.Connection    := 'Keep-Alive';
      IdHttp.Request.ContentLength := Length(sXML);

      StringToStream(sXML, SendStream);  
      SendStream.Position := 0;

      Result := IdHttp.Post(sURL, SendStream);
  finally
    IdHttp.Free;
    SendStream.Free;
  end;
end;

Addition 25.1.2009:

Exception is this: EIdConnClosedGracefully

Response is this:

<?xml version='1.0' encoding='us-ascii'?>
<!DOCTYPE Error [ <!ELEMENT Error (ErrorMessage,DebuggingInfo*)> <!ATTLIST Error Date CDATA #REQUIRED Time CDATA #REQUIRED> <!ELEMENT ErrorMessage (#PCDATA )>  <!ELEMENT DebuggingInfo (#PCDATA )> ] >
<Error Date='01/25/2010' Time='08:57:12'>
<ErrorMessage>
    XML SERVER ERROR: There was a SYSTEM ERROR error in the Incoming XML Response: $ZE=&lt;UNDEFINED&gt;lexan+196^%eXMLLexAnalyzer
</ErrorMessage>

Solution 26.1.2009:

function CallRpc(const sURL, sXML: string): string; 
var 
  SendStream : TStream; 
  IdHttp     : TIdHttp; 
  sAnsiXML: Ansistring;   // <-- new
begin 
  sAnsiXML := sXML;  // <-- new: Implicit string cast

  SendStream := TMemoryStream.Create; 
  IdHttp := TIdHttp.Create(nil); 
  try 
     IdHttp.Request.Accept        := '*/*'; 
     IdHttp.Request.ContentType   := 'text/sXML'; 
     IdHttp.Request.Connection    := 'Keep-Alive'; 
     IdHttp.Request.ContentLength := Length(sAnsiXML); // <-- new

     SendStream.Write(sAnsiXML[1], Length(sAnsiXML));  // <-- new
     SendStream.Position := 0; 

     Result := IdHttp.Post(sURL, SendStream); 
  finally 
   IdHttp.Free; 
   SendStream.Free; 
  end; 

end;

+1  A: 

see if making sXML ansistring makes a difference.

Maybe the string is streamed in UTF-16 or so.

Marco van de Voort
Do you mean that D2007 string is different than D2010 string?
pKarelian
Yes. D2009+ is unicode, IIRC I had to copy stringtostream too, and adapt it to ansistring, there was no ansistring stringtostream in D2009+. And I wanted my filetype remain the same and compatible.
Marco van de Voort
Thanks. I will try fix my code.
pKarelian
Karelian, the change in string types is **the major** difference between Delphi 2010 and Delphi 2007. If you missed that, you probably missed a lot of other things. It would be a good idea for you to go read the release notes.
Rob Kennedy
OK. But this new unicode string causes so hidden effect to IdHttp Post. I fear what other surprises there will be. Compability of my old codes must be carefully checked with Delphi and Indy release notes .
pKarelian
Other surprise I found in D2010: RadioGroup behaves oddly. It seems that I can not set it's Radiobuttons properties in runtime.
pKarelian
If you want the "OLD" stings you can use AnsiString
Ritsaert Hornstra
I have to use ansistrings, if Indy IhHttp cannot handle new unicode string. Nobody told to Indy people about Delphi unicode strings? :-)
pKarelian
Implicit conversion unicode string to ansistring fixed the problem. Thanks Marco!
pKarelian
+4  A: 

The contentLength is in octets and your string length is in Chars. Since sizeof( Char ) = 2 in Delphi 2009+ this is a mismatch!

Perhaps it is better to convert your XML to/from an UTF8 string. Some aplications will not support the USC2 Unicode format.

You should supply the size of the resulting stream as the ContentLength.

Better yet: don't supply the ContentLength and let Indy do it for you..

Ritsaert Hornstra
Thanks. Good hints.
pKarelian