title: idhttp - get response string while loading page
body: <p>I need to get received string before page is loaded (to use with asterix http AMI events).
  So i am trying to access received string in OnWork event of idHttp, but I am getting error:</p>
  
  <pre><code>var
    Form2: TForm2;
    s:TStringStream;
  
  procedure TForm2.Button1Click(Sender: TObject);
  begin
    s:=TStringStream.Create;
    idhttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&amp;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;
  </code></pre>
  
  <p>UPDATE:
  I created custom class (TAMIStringStream) as Remy Lebeau adviced, but still getting an error. What am I doiung wrong?</p>
  
  <pre><code>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&amp;username=1cami&amp;secret=111');
    s:=TAMIStringStream.Create;
    while cxCheckBox1.Checked do begin
      idhttp1.Get('http://website.com:8088/asterisk/rawman?action=waitevent&amp;timeout=10',s);
    end;
  end;
  
  end.
  </code></pre>