TsgcHTTP2Client | Metodo HTTP/2 per le richieste

Il Client HTTP/2 può operare in modalità bloccante e non bloccante; internamente il componente lavora in un thread secondario e le richieste vengono elaborate in modo asincrono, ma è possibile effettuare una richiesta e attendere fino al suo completamento.

 

Di seguito è riportato un esempio di come il client può richiedere una pagina HTML a un server HTTP/2 e come può funzionare in entrambe le modalità.

Modalità asincrona

Ottenga il seguente URL: https://www.google.com e venga notificato quando il client riceve la risposta completa. Dopo aver chiamato il metodo GETASYNC, il processo continua e l'evento OnHTTP2Response viene richiamato quando la risposta viene ricevuta.

 


oClient := TsgcHTTP2Client.Create(nil);
oClient.OnHTTP2Response := OnHTTP2ResponseEvent;
oClient.GetAsync('https://www.gooogle.com');
procedure OnHTTP2ResponseEvent(Sender: TObject; const Connection: TsgcHTTP2ConnectionClient; 
  const Request: TsgcHTTP2RequestProperty; const Response: TsgcHTTP2ResponseProperty);
begin
  ShowMessage(Response.Headers.Text + #13#10 + Response.DataString);
end;

 

Modalità Bloccante

Ottiene il seguente url: https://www.google.com e attende finché il client riceve la risposta completa. Dopo aver chiamato il metodo GET, il processo attende finché non viene ricevuta la risposta o non viene raggiunto il time out.

Può accedere ai dati Raw Response usando la proprietà Response del client HTTP/2. Qui può accedere a Raw Headers, codice di stato della risposta, Charset e altro.

 


oClient := TsgcHTTP2Client.Create(nil);
vResponse := oClient.Get('https://www.gooogle.com');
if oClient.Response.Status = 200 then
  ShowMessage('Response from server: ' + vResponse)
else
  ShowMessage('Response Code: ' + IntToStr(oClient.Response.Status));