Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Ayuda con TidHTTP (https://www.clubdelphi.com/foros/showthread.php?t=75559)

JuanOrtega 02-09-2011 01:25:31

Ayuda con TidHTTP
 
Ando teniendo una duda sobre un codigo que vi en este mismo foro , el codigo es el siguiente

Código:

procedure TForm1.VerificarClick(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  Response: String;
begin

  IdHTTP := nil;
  try

    IdHTTP := TIdHTTP.Create(nil);
    IdHTTP.Get(Trim(Edit1.Text));

    (* el edit1 tiene el link de descarga *)
    Response := IdHTTP.Get(Trim(Edit1.Text));

    Memo1.Text:= Response;

    if  IdHTTP.ResponseCode=404 then begin
    Label1.Caption:='link rroto';
  end else
    Label1.Caption:='link bueno';

  finally
    IdHTTP.Free;

  end;

end;

Mi duda es que cuando la pagina no existe el programa muestra error y se para ahi , mi duda es si se pueda evitar la ventana que dice "HTTP /1.1 404 Not Found." , que el error no se muestre y que el programa continue con sus labores como si nada hubiera pasado para poder seguir cargando otras paginas usando TidHTTP.


Perdon si no me exprese bien...

dec 02-09-2011 19:29:18

Hola,

Puedes "capturar" las posibles excepciones que lance el objeto "IdHttp", por ejemplo:

Código Delphi [-]
var
  http: TIdHttp;
begin
  http := TIdHttp.Create( nil );
  try
    try
      Memo1.Text:= http.Get( Trim( Edit1.Text ) );
    except
      // Por ejemplo "HTTP not found"
      on E : EIdHttpProtocolException do
      begin
        ShowMessage( E.Message );
      end;
      // Cualquier otro tipo de excepción
      on E : Exception do
      begin
        ShowMessage( E.Message );
      end;
    end;
  finally
    http.Free();
  end;
end;

JuanOrtega 04-09-2011 20:10:31

Gracias por la respuesta me ha servido de mucho , pero ahora tengo otra duda porque tengo un array que verifica la existencia de todos los archivos en el array pero cuando
una pagina no existe automaticamente no sigue con los otros elementos de la lista y deja de escanear.

Código:

procedure TForm1.Button1Click(Sender: TObject);
const
  paginas:array[1..4] of string = ('index.php','noexisto.php','basura/noexisto','index.php');

var
  IdHTTP: TIdHTTP;
  i:integer;

begin


  try
    for i:= low(paginas) to High(paginas) do
    begin

    IdHTTP := nil;
    IdHTTP := TIdHTTP.Create(nil);
    IdHTTP.Get(Edit1.Text+'/'+paginas[i]);

    if  IdHTTP.ResponseCode=200 then begin
    Memo1.Lines.Add(Edit1.Text+'/'+paginas[i]);
    end;
    end;
    except
      on E : EIdHttpProtocolException do
      begin
      end;
      on E : Exception do
      begin
      end;
    end;
    end;

¿ Me podrias ayudar a arreglar el codigo ?

Chris 04-09-2011 22:05:58

Meté el try dentro del for:

Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
const
    paginas:array[1..4] of string = ('index.php','noexisto.php','basura/noexisto','index.php');

var
    IdHTTP: TIdHTTP;
    i:integer;
begin
    try
        IdHTTP := TIdHTTP.Create(nil);
        
        for i:= Low(paginas) to High(paginas) do
        try
            IdHTTP.Get(Edit1.Text+'/'+paginas[i]);

            if  IdHTTP.ResponseCode=200 then
                Memo1.Lines.Add(Edit1.Text+'/'+paginas[i]);
        except
            on E : EIdHttpProtocolException do {nothing!};
            on E : Exception do {nothing!};
        end;
    finally
        idHTTP.Free;
    end;
end;

He aplicado una pequeña reestructuración de tu código y he reparado una fuga de memoria que tenías en el. No estabas liberando el objeto idHTTP.

Saludos,
Chris

dec 05-09-2011 02:49:03

Hola,

Y saca el "Create" fuera del "Try":

Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
const
    paginas:array[1..4] of string = ('index.php','noexisto.php','basura/noexisto','index.php');

var
    IdHTTP: TIdHTTP;
    i:integer;
begin
    IdHTTP := TIdHTTP.Create(nil);
    try
        
        for i:= Low(paginas) to High(paginas) do
        try
            IdHTTP.Get(Edit1.Text+'/'+paginas[i]);

            if  IdHTTP.ResponseCode=200 then
                Memo1.Lines.Add(Edit1.Text+'/'+paginas[i]);
        except
            on E : EIdHttpProtocolException do {nothing!};
            on E : Exception do {nothing!};
        end;
    finally
        idHTTP.Free;
    end;
end;

¡Esto sí que es un mete-saca! :D

JuanOrtega 05-09-2011 23:59:32

gracias a los dos por la ayuda , me ha servido de mucho , pensar que hace un tiempo pense que esto era imposible xDDD.
Pero gracias a la magia de indy y la ayuda de ustedes esto es posible.


La franja horaria es GMT +2. Ahora son las 12:24:39.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi