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