Search code examples
delphi-xevclidhttp

How to catch 404 error and and find what causing it from listbox


i want to catch to 404 error exception and catch the item that causing this error from the listbox1 and store it in listbox2!

my code so far :

procedure TForm1.Button3Click(Sender: TObject);
var
s: string;
lHTTP: TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
i: Integer;
satir: Integer;
str: TStringList;
begin

  lHTTP := TIdHTTP.Create(nil);
  IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
 try
 lHTTP.ReadTimeout := 30000;
 lHTTP.IOHandler := IdSSL;
 IdSSL.SSLOptions.Method := sslvTLSv1;
 IdSSL.SSLOptions.Method := sslvTLSv1;
 IdSSL.SSLOptions.Mode := sslmUnassigned;
 lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
 lHTTP.HandleRedirects := True;
 satir := ListBox1.Items.Count;
 str := TStringList.Create;
 for i:= 0 to satir-1 do
 begin

 try
 lHTTP.Get(ListBox1.Items.Strings[i]);
  except
  on E: EIdHTTPProtocolException do
   begin
   if E.ErrorCode <> 404 then

    raise;
   Break;
   Memo1.Lines.Add(E.ErrorMessage);
 end;
 end;

 end;

Finally

end;
end;

now when i press button3 nothing is added to memo1 i need help please, Many thanks in advanace.


Solution

  • Just add the URL that failed (from ListBox1) to ListBox2 when the exception occurs:

    try
      lHTTP.Get(ListBox1.Items[i]);
    except
      on E: EIdHTTPProtocolException do
      begin
        if E.ErrorCode <> 404 then 
          raise;
        Memo1.Lines.Add(E.ErrorMessage);
        ListBox2.Items.Add(ListBox1.Items[i]);
     end;