Tuesday, April 28, 2015

XE8 native HTTP client and a small defect

In XE8 there is unit System.Net.HttpClient added. Which is good in my opinion, as it don't use Indy layer and calls directly Windows APIs. So it must be better for both performance and stability.

In general there are classes/interfaces for web Request, Response and HttpClient.

BTW GetIt functionality uses THTTPClient internally.

Here's a minimal example:

uses
  System.Net.HttpClient;

var
  client: THTTPClient;
  response: IHTTPResponse;

begin
  client := THTTPClient.Create;
  try
    response := client.Get('http://google.com');
    if response.StatusCode = 200 then
    begin
      // do something
    end;
  finally
    client.Free;
  end;
end.

Quite easy and simple.

Though there's one defect I found.

if you evaluate response.StatusText it will be OK#0, yeah, that trailing #0 from Windows call.

To get response content you either pass your stream to Get function or use reponse.ContentStream.

POST method is similar.

No comments:

Post a Comment