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.
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
(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).