Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

http - Delphi: Indy - how to get the response body on error?

Delphi6 and XE3.

I want to get the real response body of a request. But the server makes error 500. Then the Indy replace the response text with the description of the error.

This homepage is designed for answer 500 in non abnormal operations too, and we need to determine what to do from response text.

Could I get the response text from IdHTTP or from an Exception object when the status code is 500?

Thanks for any info!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When TIdHTTP encounters a server error, it raises an EIdHTTPProtocolException exception, where its ErrorCode property contains the HTTP status code (500, etc), its Message property contains the HTTP status text ("Internal Error", etc), and its ErrorMessage property contains the body text of the response, if any. So, for example:

try
  IdHTTP1.Get(...);
except
  on E: EIdHTTPProtocolException do begin
    // use E.ErrorCode, E.Message, and E.ErrorMessage as needed...
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...