Answer the question
In order to leave comments, you need to log in
Delphi console application async call proc?
Greetings!
Console application.
Embarcadero® Delphi 10.3 Version 26.0.34749.6593
Created a test class, I want to make an asynchronous call to a certain piece of code.
Why do I need it?
I am planning to use this kind of structure
Http.Get(AUrl).BeforeRequest(
procedure(AURL: string; Stream: TStream; Headers: TNetHeaders)
begin
// Перед запросом
end).AfterRequest(
procedure(ARatry: boolean; HTTPResponse: IHTTPResponse)
var
JsonObject: TJSONObject;
csrfToken: string;
begin
JsonObject := TJSONObject.ParseJSONValue(HTTPResponse.ContentAsString(TEncoding.UTF8)) as TJSONObject;
if JsonObject.TryGetValue('csrfToken', csrfToken) then
begin
// После выполнения запроса
// ARatry = true - если нам нужно повторить запрос
ARatry := true
end;
end).Success(
procedure(Content: string)
begin
// Ответ сервера status code 200
end);
unit Http;
interface
uses
System.SysUtils, System.Classes, System.Net.HttpClient, System.Net.URLClient,
Winapi.Windows, syncobjs;
type
THttp = class
FBeforeRequestProc: TProc<string, TStream, TNetHeaders>;
FAfterRequestProc: TProc<boolean, IHTTPResponse>;
FSuccessProc: TProc<string>;
protected
public
function BeforeRequest(Proc: TProc<string, TStream, TNetHeaders>): THttp;
function AfterRequest(Proc: TProc<boolean, IHTTPResponse>): THttp;
function Success(const Proc: TProc<string>): THttp;
function Get(const AURL: string; const AResponseContent: TStream = nil; const AHeaders: TNetHeaders = nil): THttp;
end;
implementation
{ THttp }
function THttp.AfterRequest(Proc: TProc<boolean, IHTTPResponse>): THttp;
begin
FAfterRequestProc := Proc;
end;
function THttp.BeforeRequest(Proc: TProc<string, TStream, TNetHeaders>): THttp;
begin
FBeforeRequestProc := Proc;
end;
function THttp.Get(const AURL: string; const AResponseContent: TStream; const AHeaders: TNetHeaders): THttp;
var
Headers: TNetHeaders;
Retry: boolean;
Response: IHTTPResponse;
THread: TTHread;
begin
Headers := AHeaders;
THread := TTHread.CreateAnonymousThread(
procedure
var
http: THttpClient;
begin
try
http := THTTPClient.Create;
FBeforeRequestProc(AURL, AResponseContent, AHeaders);
Response := http.get(AURL, AResponseContent, AHeaders);
FAfterRequestProc(Retry, Response);
if Response.StatusCode = 200 then
FSuccessProc(Response.ContentAsString(TEncoding.UTF8));
finally
FreeAndNil(http);
end;
end);
THread.Resume;
end;
function THttp.Success(const Proc: TProc<string>): THttp;
begin
FSuccessProc := Proc;
end;
end.
function THttp.Get(const AURL: string; const AResponseContent: TStream; const AHeaders: TNetHeaders): THttp;
var
Headers: TNetHeaders;
Retry: boolean;
Response: IHTTPResponse;
THread: TTHread;
begin
Headers := AHeaders;
THread := TTHread.CreateAnonymousThread(
procedure
var
http: THttpClient;
begin
try
http := THTTPClient.Create;
FBeforeRequestProc(AURL, AResponseContent, AHeaders);
Response := http.get(AURL, AResponseContent, AHeaders);
FAfterRequestProc(Retry, Response);
if Response.StatusCode = 200 then
FSuccessProc(Response.ContentAsString(TEncoding.UTF8));
finally
FreeAndNil(http);
end;
end);
THread.Resume;
end;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question