HTTP/2 客户端可以工作在阻塞和非阻塞模式下。组件内部在辅助线程中工作,请求以异步方式处理,但您可以发起请求并等待其完成。
以下是一个示例,说明客户端如何向 HTTP/2 服务器请求 HTML 页面,以及如何在两种模式下工作。
获取以下 URL:https://www.google.com,并在客户端接收完整响应时收到通知。调用 GETASYNC 方法后,处理过程继续执行,接收到响应时触发 OnHTTP2Response 事件。
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;
获取以下 URL:https://www.google.com,并等待客户端接收到完整响应。调用 GET 方法后,进程将等待直到收到响应或超时。
您可以通过 HTTP/2 客户端的 Response 属性访问原始响应数据,包括原始头、状态响应码、字符集等更多信息。
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));