Using Tidhttp and Tidconnectionintercept created in Runtime

Asked

Viewed 763 times

1

I have a method that makes a connection to a Restful server and for this uses the components Tidhttp, Tidconnectionintercept and others created in Runtime, so far so good, the problem is that I need to get the return of the event "Onreceive" of the Tidconnectionintercept component and I don’t know how to do this, see the code:

procedure ConHttpThread;
    var
     IdHTTPLibSysLoc : TIdHTTP;
     IdCookieManagerLibSysLoc : TIdCookieManager;
     IdConnectionInterceptLibSysLoc : TIdConnectionIntercept;
     AuthSSL : TIdSSLIOHandlerSocketOpenSSL;
     IdLogDebug1Loc : TIdLogDebug;
     sHtmlResp, sHtmlErro : String;
   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
      sHtmlResp := IdHTTPLibSysLoc.Post(xUrlPost, paramsLog);
    except
      sHtmlErro := ''; //variável s do evento IdConnectionInterceptLibSysLocReceive
    end;
    IdHTTPLibSysLoc.Disconnect;
   end;

Component event

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;

The event is called when doing Tidhttp.Post(), and when the server returns an error it goes straight to Except, I can’t use a glogal variable because this main method is mult thread, some idea of how to pick up the return after the Post?

Thank you!

  • why instead of using global variable, do not make a class responsible for that process and create the variable in that class?

  • Try removing the "nil" "Idhttplibsysloc := Tidhttp.Create;" and ending with "Idhttplibsysloc.Free;", as you disconnected.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.