http OnHTTPError event
HTTP Status Codes
Return to Introduction  Previous page  Next page
Applies to
auHTTP component.  

Declaration
type  
  TauHTTPErrorEvent = procedure(Sender: TObject;  
    ErrorCode: Integer; Stream: TStream) of object;  
 
property OnHTTPError: TauHTTPErrorEvent;  

Description
The OnHTTPError event occurs if some error code has received in the header of response from HTTP server.  
 
ErrorCode parameter contains the number which identifies the HTTP error (see the list of HTTP Status Codes to recognize an error).  
 
Stream contains the error page generated by server.  
 
iiinfo Most often errors is 404 (requested document not found), 403 (view forbidden) and 500 (CGI script failed).  
 
tip To handle error #401 (Access denied / Password required required to access) — write OnPasswordRequest event handler. (If OnPasswordRequest event handler exists, OnHTTPError will not occurs on error 401.)  
 
tiptip Alternatively you can get HTTP error code in OnHeaderInfo event handler and decide whether to continue download (error page), or not, so this will save some client's bandwidth from downloading error page from server.  

Remarks
iiwarning If it returns 0 in ErrorCode paramter, this means that component for some reason is unable to determinate the status code of HTTP query. However this is not always means fatal error like OnHostUnreachable. It's possible that server simply did not sent the status code in the header of response.  

Example
Delphi:  
procedure TForm1.auHTTP1HTTPError(Sender: TObject;  
  ErrorCode: Integer; Stream: TStream);  
var  
  Str: String;  
begin  
  with Stream as TMemoryStream do  
   if OutToMemo1.Checked then  
    begin // Output to Memo1  
     SetLength(Str, Size);  
     Move(Memory^, Str[1], Size);  
     Memo1.Text := Str;  
    end  
   else // Save to file  
    begin  
     Memo1.Text := 'Saved to c:\httptest.dat';  
     SaveToFile('c:\httptest.dat');  
    end;  
 
  case ErrorCode of  
    404: Str := '404: Document not found';  
    500: Str := '500: CGI script failed';  
    else // Mysterious reason  
      Str := IntToStr(ErrorCode);  
   end;  
 
  if (ErrorCode = HTTP_STATUS_OK) or (ErrorCode = HTTP_STATUS_PARTIAL_CONTENT) then // consts from WinInet.pas  
   begin  
    ContinueDownload := False;  
    Exit;  
   end;  
 
  StatusBar1.Panels[0].Text := 'HTTP Error #' + Str;  
end;  



C++ Builder:  
void __fastcall TForm1::auHTTP1HTTPError(TObject *Sender,  
   int ErrorCode, TStream *Stream)  
{  
  AnsiString Str;  
 
  if (OutToMemo1->Checked) { // Output to Memo1  
    Str.SetLength(Stream->Size);  
    Move(((TMemoryStream*)Stream)->Memory, &Str[1], Stream->Size);  
    Memo1->Text = Str;  
  }else { // Save to file  
    Memo1->Text = "Saved to c:\httptest.dat";  
    ((TMemoryStream*)Stream)->SaveToFile("c:\httptest.dat");  
  }  
 
  switch (ErrorCode) {  
    case 404: Str = "404: Document not found";  
    case 500: Str = "500: CGI script failed";  
    default:  Str = IntToStr(ErrorCode); // Mysterious reason  
  };  
 
  StatusBar1->Panels->Items[0]->Text = "HTTP Error #" + Str;  
}  

See also
HTTP Status Codes  
OnHeaderInfo, OnPasswordRequest, OnAnyError and OnDone events.  
File not found.