0

我需要在页面加载之前接收到字符串(用于 asterix http AMI 事件)。所以我试图在 idHttp 的 OnWork 事件中访问接收到的字符串,但出现错误:

var
  Form2: TForm2;
  s:TStringStream;

procedure TForm2.Button1Click(Sender: TObject);
begin
  s:=TStringStream.Create;
  idhttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&timeout=10',s);
  showmessage(s.DataString); //NO ERROR
end;

procedure TForm2.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Int64);
begin
      showmessage(s.DataString); //ERROR HERE
end;

更新:我按照 Remy Lebeau 的建议创建了自定义类 (TAMIStringStream),但仍然出现错误。我做错了什么?

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, Vcl.StdCtrls, cxTextEdit, cxMemo,
  cxCheckBox;

type
  TAMIStringStream = class(TStringStream)
    FEncoding: TEncoding;
    public
    ReceivedSTR:string;

    function Write(const Buffer; Count: Longint): Longint; override;
  end;

  TForm2 = class(TForm)
    IdHTTP1: TIdHTTP;
    Button1: TButton;
    cxCheckBox1: TcxCheckBox;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  s:TAMIStringStream;

implementation

{$R *.dfm}

function TAMIStringStream.Write(const Buffer; Count: Longint): Longint;
var t:string;
begin
  Inherited;

  t := FEncoding.GetString(Bytes, Position - Count, Count);
  form2.memo1.lines.add(t);
  ReceivedSTR := ReceivedSTR + t;
end;


procedure TForm2.Button1Click(Sender: TObject);
begin
  idhttp1.Get('http://website.com:8088/asterisk/rawman?action=login&username=1cami&secret=111');
  s:=TAMIStringStream.Create;
  while cxCheckBox1.Checked do begin
    idhttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&timeout=10',s);
  end;
end;

end.
4

1 回答 1

3

要在 仍在下载服务器的 HTTP 响应数据时获取它TIdHTTP,您需要编写自己的TStream派生类来覆盖虚拟TStream.Write()方法,然后可以将该类的实例传递给 的AResponseContent参数TIdHTTP.Get()。您的Write()方法可以在将数据“写入”到您的流中时对其进行处理(准备好以任意块处理该数据,因为它是实时流式传输的)。

否则,您将不得不TIdHTTP完全跳过并使用TIdTCPClient手动实现 HTTP 协议,以便您完全控制读取和写入。

“AMI over HTTP”协议文档(参见thisthis)展示了如何向AMI 发送HTTP 请求以及如何轮询事件(是的,您必须在使用HTTP 时轮询事件)。由于轮询在传递事件之前不会返回,因此没有太多理由在运行中读取服务器的响应数据。 TIdHTTP.Get()将阻塞直到收到事件,然后您可以根据需要对其进行处理。因此,如果没有自定义流,您的第一种方法应该没问题:

procedure TForm2.Button1Click(Sender: TObject);
var
  s: TStringStream;
begin
  idhttp1.Get('http://website.com:8088/asterisk/rawman?action=login&username=1cami&secret=111');
  s := TStringStream.Create;
  try
    while cxCheckBox1.Checked do
    begin
      IdHttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&timeout=10', s);
      Memo1.Lines.Add(s.DataString);
      s.Clear;
    end;
  finally
    s.Free;
  end;
end;

或者:

procedure TForm2.Button1Click(Sender: TObject);
var
  s: String;
begin
  idhttp1.Get('http://website.com:8088/asterisk/rawman?action=login&username=1cami&secret=111');
  while cxCheckBox1.Checked do
  begin
    s := IdHttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&timeout=10');
    Memo1.Lines.Add(s);
  end;
end;

由于TIdHTTP's 的阻塞性质,我建议将轮询移到工作线程中:

procedure TMyThread.Execute;
var
  http: TIdHTTP;
  s: String;
begin
  http := TIdHTTP.Create(nil);
  try
    http.Get('http://website.com:8088/asterisk/rawman?action=login&username=1cami&secret=111');
    while not Terminated do
    begin
      s := http.Get('http://website.com:8088/asterisk/rawman?action=waitevent&timeout=10');
      // do something...
    end;
  finally
    http.Free;
  end;
end;

如果 HTTP 轮询不适合您的需求,您应该考虑改用“AMI over TCP”(参见thisthis),并使用TIdTCPClient它。您可以使用计时器或线程来检查传入数据。

于 2015-08-05T23:14:21.670 回答