【问题标题】:Problem with Indy IdHttp Post in Delphi 2010Delphi 2010 中的 Indy IdHttp Post 问题
【发布时间】:2011-01-08 12:33:09
【问题描述】:

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

2009 年 1 月 25 日新增:

例外是: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>

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

结束;

【问题讨论】:

  • 欢迎来到 Stack Overflow。请记住包含重要的细节,例如您遇到的哪个异常,这样读者就不必猜测了。
  • 当我能够再次使用我的编码计算机并重复错误时,我将提供更多详细信息。

标签: delphi http unicode delphi-2010 indy


【解决方案1】:

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

也许最好将您的 XML 转换为 UTF8 字符串/从 UTF8 字符串转换。某些应用程序不支持 USC2 Unicode 格式。

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

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

【讨论】:

    【解决方案2】:

    看看制作 sXML ansistring 是否有所作为。

    也许字符串是以 UTF-16 左右的形式流式传输的。

    【讨论】:

    • 你的意思是D2007字符串和D2010字符串不同吗?
    • 是的。 D2009+ 是 unicode,IIRC 我也必须复制 stringtostream,并将其调整为 ansistring,D2009+ 中没有 ansistring stringtostream。而且我希望我的文件类型保持不变且兼容。
    • Karelian,字符串类型的变化是 Delphi 2010 和 Delphi 2007 之间的主要区别。如果你错过了这一点,你可能错过了很多其他的东西。您最好阅读发行说明。
    • 好的。但是这个新的 unicode 字符串对 IdHttp Post 造成了如此隐蔽的影响。我担心还会有什么惊喜。我的旧代码的兼容性必须仔细检查 Delphi 和 Indy 发行说明。
    • 我在 D2010 中发现的其他惊喜:RadioGroup 行为异常。看来我无法在运行时设置它的 Radiobuttons 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多