Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Saber si está arriba una URL (https://www.clubdelphi.com/foros/showthread.php?t=60610)

Faust 07-10-2008 18:37:14

Saber si está arriba una URL
 
Espero no estar abriendo un hilo con un tema ya discutido anteriormente.

La cuestión es esta: necesito monitorear cuando ya esté levantada cierta página de internet, p. ej.

Saber si "https://servidor.com.mx/directorio" me direcciona a su página por default, en este caso sería "https://servidor.com.mx/directorio/index.html" y no me devuelve un error "404 Not Found".

No me he metido mucho en esto de internet, agradecería cualquier ayuda.

Fistandantilus 29-10-2008 20:09:44

Fijate si esto te sirve.

tenes q meter la libreria WinInet

Código Delphi [-]
function TPreferencesForm.GetURLStatus(aURL: String): Integer;
var
   hInet, hConecction, httprequest: HINTERNET;
   Host, Page: String;
   size, Reserved: Cardinal;
   Head: Array [0..1024] of Char;
begin
   Result := -1;
   Reserved := 0;
   hInet := nil;
   hConecction := nil;
   httprequest := nil;
   if (aURL <> '') then begin
      try
         hInet := InternetOpen('lo q kieras', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
         if Assigned(hInet) then begin
            AnsiReplaceStr(aURL, 'http://', '');
            if (Pos('/', aURL) > 0) then begin
               Host := Copy(aURL, 1, Pos('/', aURL)-1);
               Page := Copy(aURL, Pos('/', aURL)+1, Length(aURL));
            end else begin
               Host := aURL;
               Page := '';
            end;
            hConecction := InternetConnect(hInet, PAnsiChar(Host), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
            if Assigned(hConecction) then begin
               HttpRequest := HttpOpenRequest(hConecction, 'HEAD', PAnsiChar(Page), nil, nil, nil, INTERNET_FLAG_KEEP_CONNECTION, 0);
               if Assigned(HttpRequest) then begin
                  if (HttpSendRequest(HttpRequest, nil, 0, nil, 0)) then begin
                     size := SizeOf(Head);
                     HttpQueryInfo(HttpRequest, HTTP_QUERY_STATUS_CODE, @Head, size, reserved);
                     if (String(Head) <> '') then begin
                        try
                           Result := StrToInt(String(Head));
                        except
                           Result := -6;
                        end;
                     end else begin
                        Result := 0;
                     end;
                  end else begin
                     Result := -5;
                  end;
               end else begin
                  Result := -4;
               end;
            end else begin
               Result := -3;
            end;
         end else begin
            Result := -2;
         end;
      finally
         if Assigned(hInet) then InternetCloseHandle(hInet);
         if Assigned(hConecction) then InternetCloseHandle(hConecction);
         if Assigned(HttpRequest) then InternetCloseHandle(HttpRequest);
      end;
   end;

end;

Espero q te sirva

saludos

Faust 31-10-2008 00:08:17

Gracias por tu respuesta amigo...

Ya hasta me había olvidado de este hilo :o.

En cuanto pueda lo pruebo...


La franja horaria es GMT +2. Ahora son las 05:16:09.

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