Delphi 2010 中的 Indy IdHttp Post 问题

发布于 2024-08-18 21:22:55 字数 2171 浏览 2 评论 0原文

我对 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;

添加 25.1.2009:

异常是这样的:EIdConnClosedGraceively

响应是这样的:

<?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>

解决方案 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; 

结束;

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=<UNDEFINED>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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-08-25 21:22:55

contentLength 以八位字节为单位,字符串长度以字符为单位。由于 Delphi 2009+ 中 sizeof( Char ) = 2,这是不匹配的!

也许最好将 XML 与 UTF8 字符串相互转换。某些应用程序不支持 USC2 Unicode 格式。

您应该提供结果流的大小作为 ContentLength。

更好的是:不要提供 ContentLength,让 Indy 为您做。

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..

她比我温柔 2024-08-25 21:22:55

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

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

see if making sXML ansistring makes a difference.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文