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

2

5

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?

Comeback answered 31/8, 2013 at 14:48 Comment(3)
no I mean when it gives http bad request 400 I can see its response content in Fiddler so it definitely send content but I cannot get it in delphi. delphi just gives error.Comeback
in this comment (#7763084) you say "Firefox displays the content even if he get HTTP 400 Bad Request error. E.g. Internet Explorer tell you that bad request happened and doesn't display the XML content response, Indy works the same, create the error message and throw away the content." i think my problem is exactly that.Comeback
TIdHTTP does not throw away the content. It is placed in the exception that is raised. See my answer for details.Trematode
T
7

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.

Trematode answered 31/8, 2013 at 20:41 Comment(1)
thanks for answer. when I disabled hoNoProtocolErrorException ErrorMessage gave me response data.Comeback
H
3

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.

Homoousian answered 31/8, 2013 at 18:55 Comment(3)
Then don't include the 2nd memo1.lines.add line.Whiny
ResponseText is not "content". It's just status text. I need content.Comeback
thanks for this answer. it was my mistake. when i disabled hoNoProtocolErrorException, ErrorMessage gave me response content.Comeback

© 2022 - 2024 — McMap. All rights reserved.