0

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.

ColdZer0
  • 233
  • 1
  • 4
  • 19
  • i did, thanks, now im stuck at how ti figure out how to add that item to listbox2 which causing the 404 error @KenWhite – ColdZer0 May 09 '16 at 00:16
  • You have a `break` that will take you out of the exception block. Remove it, so that the `Memo1.Lines.Add` can execute. Learn to use the debugger, and you can figure these sorts of things out faster than you can create a post here. – Ken White May 09 '16 at 00:17
  • Your code makes no effort to add anything to ListBox2, so I have no idea what problem you might be having. There's not even a ListBox2 anywhere in your code. – Ken White May 09 '16 at 00:18
  • i know, still a beginner, i have 2 ListBoxs, the first is full with URLs, now i want to detect the url which causing the 404 and store it in listbox2, any idea how ? @KenWhite – ColdZer0 May 09 '16 at 00:22

1 Answers1

0

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks buddy, any advise how to make it faster, threads maybe? – ColdZer0 May 09 '16 at 00:33
  • This is not a *let's play 20 questions in comments* site, I'm afraid. But I find it pretty unlikely that someone who couldn't figure out how to add something to a ListBox is quite ready to attempt to write multi-threaded code. You also might want to see [this topic](http://stackoverflow.com/help/someone-answers) in the help center. – Ken White May 09 '16 at 00:34
  • How would threads help? First of all ask yourself why you have a delay? Remember that threading is useful for cpu bound tasks. Is yours cpu bound? – David Heffernan May 09 '16 at 05:20
  • just to speed up checking process? any alternative other then threads ? @DavidHeffernan – ColdZer0 May 09 '16 at 14:26
  • Actually, I'm probably misleading you. The other thing that threads enable is asynchronous behaviour. So whilst the check may not be CPU bound, the network requests can be issued in parallel. So yeah, maybe threading is a good idea. Sorry. – David Heffernan May 09 '16 at 14:33