Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TIdHTTP error details
#1
Dear All; when TIdHTTP::Get method throws out an EIdHTTPProtocolException, where do I find details of the error?
For instance:

Code:
TIdHTTP *IdHTTP;
AnsiString strURL="https://....";
//...
try{
  TMemoryStream *m = new TMemoryStream;
  IdHTTP->Get(strURL,m);
  m->SaveToFile("tst.txt");
  delete m;
}catch(EIdHTTPProtocolException &e){
  ShowMessage(e.Message);//shows "HTTP/1.1 400 Bad Request"
}
Thanks in advance. Boba.
Reply
#2
1) Did you URL-Encode your AnsiString before use it in Idhttp->Get() ?
2) Is there possibly a Consent Page between your Target URL ? 

That would be an Explanation for this.

cheers

fearcry
Reply
#3
(08-16-2022, 01:23 AM)Boba TC Wrote: when TIdHTTP::Get method throws out an EIdHTTPProtocolException, where do I find details of the error?

The EIdHTTPProtocolException::Message property contains the HTTP response code and response status text, as you noticed (these are the same values that you can get from the TIdHTTP::ResponseCode and TIdHTTP::ResponseText properties).

The TIdHTTP::Response property contains the response code, status line, and headers.

By default, the EIdHTTPProtocolException::ErrorMessage property contains the body content of the HTTP response. Alternatively, if you want to receive the error body content in your TMemoryStream, you can enable the hoNoProtocolErrorException and hoWantProtocolErrorContent flags in the TIdHTTP::HTTPOptions property.

(08-16-2022, 01:23 AM)Boba TC Wrote: For instance:

Just FYI, you should be using System::String instead of AnsiString, and more importantly you are leaking the TMemoryStream if TIdHTTP::Get() or TMemoryStream::SaveToFile() throws an exception. You should protect the TMemoryStream with a try/__finally block, or a RAII wrapper like std::auto_ptr or std::unique_ptr (depending on which compiler you are using).

Reply
#4
(08-16-2022, 07:30 PM)rlebeau Wrote:
(08-16-2022, 01:23 AM)Boba TC Wrote: when TIdHTTP::Get method throws out an EIdHTTPProtocolException, where do I find details of the error?

The EIdHTTPProtocolException::Message property contains the HTTP response code and response status text, as you noticed (these are the same values that you can get from the TIdHTTP::ResponseCode and TIdHTTP::ResponseText properties).

The TIdHTTP::Response property contains the response code, status line, and headers.

By default, the EIdHTTPProtocolException::ErrorMessage property contains the body content of the HTTP response. Alternatively, if you want to receive the error body content in your TMemoryStream, you can enable the hoNoProtocolErrorException and hoWantProtocolErrorContent flags in the TIdHTTP::HTTPOptions property.

(08-16-2022, 01:23 AM)Boba TC Wrote: For instance:

Just FYI, you should be using System::String instead of AnsiString, and more importantly you are leaking the TMemoryStream if TIdHTTP::Get() or TMemoryStream::SaveToFile() throws an exception. You should protect the TMemoryStream with a try/__finally block, or a RAII wrapper like std::auto_ptr or std::unique_ptr (depending on which compiler you are using).
Reply
#5
(08-16-2022, 07:30 PM)rlebeau Wrote: By default, the EIdHTTPProtocolException::ErrorMessage property contains the body content of the HTTP response.  Alternatively, if you want to receive the error body content in your TMemoryStream, you can enable the hoNoProtocolErrorException and hoWantProtocolErrorContent flags in the TIdHTTP::HTTPOptions property....Just FYI, you should be using System::String instead of AnsiString, and more importantly you are leaking the TMemoryStream if TIdHTTP::Get() or TMemoryStream::SaveToFile() throws an exception.  You should protect the TMemoryStream with a try/__finally block, or a RAII wrapper like std::auto_ptr or std::unique_ptr (depending on which compiler you are using).


Thank you, Remy.
Thank you, Remy.
Thank you, Remy.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)