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

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

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.  

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.  

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

C++ Builder:  
void __fastcall TForm1::acHTTP1HTTPError(TObject *Sender,  
   int ErrorCode, TStream *Stream)  
  AnsiString Str;  
  if (OutToMemo1->Checked) { // Output to Memo1  
    Move(((TMemoryStream*)Stream)->Memory, &Str[1], Stream->Size);  
    Memo1->Text = Str;  
  }else { // Save to file  
    Memo1->Text = "Saved to 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, OnDone and OnHostUnreachable events.