How to Catch HTTP Error Response

There are two ways of handing of errors in the HTTP Client component.

1. You can set the SilentHTTP property to True and analyze the StatusCode property. This method allows you to get the server response in the provided destination stream:

clHttp.SilentHTTP := True;
clHttp.Get('http....', destination);

if (clHttp.StatusCode >= 400) then
    //the  destination parameter contains the server response

2. You can use the try-except blocks to handle the exception:

try
  clHttp.Get('http....', destination);
except
  on E: EclHttpError do
  begin
    E.ErrorCode...
    E.ResponseText...
  end;
  on E: EclSocketError do
  begin
    E.ErrorCode...
  end;
  on E: Exception do
  begin
    //...other errors
  end;
end;

Add Feedback