How to get response content even if connection gives error in IdHttp?

6.3k views Asked by At

When using TIdHttp like this:

Memo1.Text := IdHTTP1.post(url,data);

I can get response content to memo1 if it doesn't give http error. But when it gives http bad request, Indy doesn't give me content. I'm also using try..except but it only prevent error box and still doesn't give me content.

How can I get content even it returns http error?

2

There are 2 answers

1
Remy Lebeau On

When an HTTP error occurs, TIdHTTP raises an EIdHTTPProtocolException exception. That exception contains the HTTP status code in its ErrorCode property, the HTTP status text in its Message property, and the response data in its ErrorMessage property.

3
TheWesDias On

try this code

Try
    Memo1.Text := IdHTTP1.post(url,data);
except on e: EIdHTTPProtocolException do
begin
    memo1.lines.add(idHTTP1.response.ResponseText);
    memo1.lines.add(e.ErrorMessage);
end;

e.ErrorMessage will give you some informations about the bad request.