使用在运行时创建的IdHttp和IdConnectionIntercept

时间:2016-05-05 12:36:57

标签: multithreading delphi runtime idhttp

我有一个与RESTful服务器建立连接的方法,它使用TIdHttp组件,TIdConnectionIntercept和其他在运行时创建的组件,到目前为止一直很好,问题是我需要获得事件的返回&# 34;的onReceive" TIdConnectionIntercept组件,我试图传递一个变量" out"在事件参数中,大多数都不起作用:

procedure ConHttpThread;
    var
     IdHTTPLibSysLoc : TIdHTTP;
     IdCookieManagerLibSysLoc : TIdCookieManager;
     IdConnectionInterceptLibSysLoc : TIdConnectionIntercept;
     AuthSSL : TIdSSLIOHandlerSocketOpenSSL;
     IdLogDebug1Loc : TIdLogDebug;
     sHtmlResp, sHtmlRet, xUrlPost : String;
     paramsLog : TStringList;
   begin
    AuthSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    IdLogDebug1Loc := TIdLogDebug.Create(nil);
    IdCookieManagerLibSysLoc := TIdCookieManager.Create(nil);
    IdConnectionInterceptLibSysLoc := TIdConnectionIntercept.Create;
    IdConnectionInterceptLibSysLoc.Intercept := IdLogDebug1Loc;
    IdConnectionInterceptLibSysLoc.OnReceive := IdConnectionInterceptLibSysLocReceive;
    IdHTTPLibSysLoc := TIdHTTP.Create(nil);

    try
      xUrlPost := 'url...';
      paramsLog := TStringList.Create;
      paramsLog.Add('user=' + user);
      paramsLog.Add('pass=' + xSenhaToken);

      sHtmlResp := IdHTTPLibSysLoc.Post(xUrlPost, paramsLog);
      sHtmlRet := ''; //variable s event IdConnectionInterceptLibSysLocReceive
    except
      sHtmlRet := ''; //variable s event IdConnectionInterceptLibSysLocReceive
    end;
    IdHTTPLibSysLoc.Disconnect;
   end;

OnReceive Event组件" IdConnectionInterceptLibSysLoc"

procedure TFPrin.IdConnectionInterceptLibSysLocReceive(
  ASender: TIdConnectionIntercept; var ABuffer: TIdBytes);
var
  i: Integer;
  s: String;
begin
  s := '';
  for i := Low(ABuffer) to High(ABuffer) do
    s := s + UTF8Encode(chr(ABuffer[i]));
end;

当TidHttp.Post()时调用该事件,并且当服务器返回错误时,它直接进入Except,我不能使用变量glogal,主要方法" ConHttpThread"是多线程,它被称为事件" ConnectionDataAvailable"组件" TWSocketServer"任何想法如何在邮政后获得回报?

  { TClientConnection is used to handle client connections }
  TClientConnection = class(TWSocketClient)
  protected
      FRcvdLine : String;         { Buffer for commands              }
      FIndex    : Integer;        { Slot number as known by server   }
      ConnectTime  : TDateTime;
      procedure ConnectionDataAvailable(Sender: TObject; Error : Word);
  public
      constructor Create(AOwner : TComponent); override;
  end;

...

constructor TClientConnection.Create(AOwner : TComponent);
begin
    inherited Create(AOwner);
    ConnectTime       := now;
    Banner            := '';
    OnDataAvailable := ConnectionDataAvailable;
end;

...

procedure TClientConnection.ConnectionDataAvailable(
    Sender: TObject;
    Error : Word);
var
Command : String;
begin
  Command := ReceiveStr;
  ConHttpThread; //Restful connection method
end;

0 个答案:

没有答案