遇到XML-RPC和Delphi问题

时间:2010-09-02 03:27:29

标签: delphi delphi-2009 delphi-2010 xml-rpc

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdIntercept, IdCookieManager, IdZLibCompressorBase, IdCompressorZLib, IdSSL,
  IdSSLOpenSSL;

type
  TForm2 = class(TForm)
    IdHTTP1: TIdHTTP;
    Button1: TButton;
    Memo1: TMemo; 
    IdCompressorZLib1: TIdCompressorZLib;
    IdCookieManager1: TIdCookieManager;
    IdConnectionIntercept1: TIdConnectionIntercept;
    IdIOHandlerStack1: TIdIOHandlerStack;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure StringToStream(const Text: string; Stream: TStream);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.StringToStream(const Text: string; Stream: TStream);
begin
  Stream.Write(Text[1], Length(Text));
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  temp:string;
  SendStream: TStream;
  ResponseStream: TStream;
begin
    SendStream := TMemoryStream.Create;
    ResponseStream := TMemoryStream.Create;


    temp:=  '<?xml version="1.0"?>
<methodCall>
   <methodName>weblogUpdates.ping</methodName>
   <params>
      <param>
         <value>%WEBNAME%</value>
         </param>
      <param>
        <value>%WEBADDREESS%</value>
      </param>
      </params>
   </methodCall>'; // copied from text file where I was loading this

    temp:= StringReplace(temp, '%WEBNAME%', 'Generic Website Title',[rfReplaceAll, rfIgnoreCase]);
    temp:= StringReplace(temp, '%WEBADDREESS%', 'http://www.testingwebsite.com',[rfReplaceAll, rfIgnoreCase]);

    memo1.Lines.Add(temp);
    StringToStream(temp, SendStream); // convert to a stream
    SendStream.Position := 0;

    idhttp1.Request.Accept := '*/*';
    idhttp1.Request.ContentType := 'text/xml';
    idhttp1.Request.Connection := 'Keep-Alive';
    idhttp1.Request.ContentLength := Length(temp);
    memo1.lines.Add(idhttp1.Post('http://ping.feedburner.com', SendStream));
{
if FHostPort = 80 then
          Session.Post('http://' + FHostName + FEndPoint, SendStream,
            ResponseStream)
        else
          Session.Post('http://' + FHostName + ':' + IntToStr(FHostPort) +
            FEndPoint, SendStream, ResponseStream);

      if FSSLEnable then
        Session.Post('https://' + FHostName + ':' + IntToStr(FHostPort) +
          FEndPoint, SendStream, ResponseStream);

}
end;

end.
在DFM上,我在idHTTP1下设置了压缩器,CookieManager,Intercept和IOHandler。 我将所需的OpenSSL dll文件复制到项目文件夹

继续收到错误: 无法解析XML-RPC请求:XML文档结构必须在同一实体中开始和结束

有关如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:2)

这是一个xml解析器异常,可能是服务器端抛出的。

因为您发送的xml是有效的(但您确实应该使用xml库来构建它!),错误很可能意味着服务器正在接收的流被截断...换句话说, content-length标头小于发送的字节数。

我的猜测是,当您使用启用了unicode的delphi版本时,您的内容长度将设置为发送的实际字节数的一半;将TextStringToStream的数据类型更改为AnsiString

答案 1 :(得分:0)

或 ... Stream.Write(Text [1],(Length(Text)* SizeOf(Char))); ... temp:='... encoding =“UTF-16”?&gt;