【问题标题】:idhttp - get response string while loading pageidhttp - 加载页面时获取响应字符串
【发布时间】:2015-08-06 23:05:30
【问题描述】:

我需要在页面加载之前接收到字符串(用于 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.

【问题讨论】:

  • 您遇到什么错误? FWIW,在可能重复触发的事件处理程序中显示模式窗口不是一个好主意。
  • 已将消息更改为备忘录。错误:“地址访问冲突......”

标签: string delphi idhttp


【解决方案1】:

要在TIdHTTP 仍在下载服务器的HTTP 响应数据时获取它,您需要编写自己的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 的阻塞性质,我建议将轮询移到工作线程中:

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。您可以使用计时器或线程来检查传入数据。

【讨论】:

  • 谢谢。我创建了自定义类(请参阅上面的更新),但仍然出现错误。我做错了什么?
  • 你没有说你得到了什么错误。但是,您的 Write() 方法正在访问您尚未初始化的 FEncoding 对象。此外,Write() 接收任意数据块,因此您不应该盲目地将它们原样转换为字符集解码的字符串。想象一下,如果事件数据包含一个 Unicode 字符,其字节跨越多个 Write() 调用会发生什么。它无法正确解码。
猜你喜欢
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多