Delphi 2010中Indy IdHttp Post的问题

pKa*_*ian 5 delphi unicode http indy delphi-2010

我有Indy IdHttp Post方法的问题.使用Delphi 2007编译的函数CallRpc()工作正常,但使用Delphi 2010编译的相同代码引发了异常.

当我将Delphi 2007 Indy TIdHttp更改为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;
Run Code Online (Sandbox Code Playgroud)

增加25.1.2009:

例外是:EIdConnClosedGracefully

回应是这样的:

<?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>
Run Code Online (Sandbox Code Playgroud)

解决方案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; 
Run Code Online (Sandbox Code Playgroud)

结束;

Mar*_*ort 1

看看使用 sXML ansisstring 是否会有所不同。

也许该字符串以 UTF-16 左右的格式进行流式传输。

  • 是的。D2009+是unicode,IIRC我也必须复制stringtostream,并将其适应ansistring,D2009+中没有ansiststring stringtostream。我希望我的文件类型保持相同且兼容。 (2认同)