Thema: Delphi Funktion URLExists?

Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#5

Re: Funktion URLExists?

  Alt 4. Aug 2007, 10:29
http://www.cryer.co.uk/brian/delphi/...isurlvalid.htm

Delphi-Quellcode:
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, WinINet;



function IsUrlValid(const url: string): boolean;
var
  hInet: HINTERNET;
  hConnect: HINTERNET;
  infoBuffer: array [0..512] of char;
  dummy: DWORD;
  bufLen: DWORD;
  okay: LongBool;
  reply: String;
begin
  hInet := InternetOpen(PChar(application.title),
    INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,nil,nil,0);
  hConnect := InternetOpenUrl(hInet,PChar(url),nil,0,
    INTERNET_FLAG_NO_UI,0);
  if not Assigned(hConnect) then
    //----------------------------------------------------------
    // If we couldn't open a connection then we know the url
    // is bad. The most likely reason is that the url is bad,
    // but it could be because of an unknown or badly specified
    // protocol.
    //----------------------------------------------------------
    result := false
  else
  begin // Create a request for the url.
    dummy := 0;
    bufLen := Length(infoBuffer);
    okay := HttpQueryInfo(hConnect,HTTP_QUERY_STATUS_CODE,
      @infoBuffer[0],bufLen,dummy);
    if not okay then
      // Probably working offline, or no internet connection.
      result := False
    else
    begin
      reply := infoBuffer;
      if reply = '200then
      begin
        Form1.Memo1.Lines.Add('200');    // File exists, all ok.
        result := True; end
      else if reply = '401then
       begin
        Form1.Memo1.Lines.Add('401');    // Not authorised. Assume page exists,
                    // but we can't check it.
        result := True; end
      else if reply = '404then
       begin
        Form1.Memo1.Lines.Add('404');   // No such file.
        result := False; end
      else if reply = '500then
       begin
        Form1.Memo1.Lines.Add('500');   // Internal server error.
        result := False; end
      else
        // Shouldn't get here! It means there is
        // a status code left unhandled.
        result := False;
    end;
    InternetCloseHandle(hConnect);
  end;
  InternetCloseHandle(hInet);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if IsUrlValid('http://www.alcpu.com/CoreTemp/CoreTemp.zip') then
 Memo1.Lines.Add('CORETEMP.zip exists');
end;
(*
    HTTP_STATUS_OK                  200
    HTTP_STATUS_CREATED            201
    HTTP_STATUS_ACCEPTED            202
    HTTP_STATUS_NO_CONTENT          204
    HTTP_STATUS_MOVED_PERM          301
    HTTP_STATUS_MOVED_TEMP          302
    HTTP_STATUS_NOT_MODIFIED        304
    HTTP_STATUS_USE_PROXY          305
    HTTP_STATUS_BAD_REQUEST        400
    HTTP_STATUS_UNAUTHORIZED        401
    HTTP_STATUS_FORBIDDEN          403
    HTTP_STATUS_NOT_FOUND          404
    HTTP_STATUS_METHOD_NOT_ALLOWED  405
    HTTP_STATUS_PROXY_AUTH_REQRD    407
    HTTP_STATUS_LENGTH_REQUIRED    411
    HTTP_STATUS_SERVER_ERROR        500
    HTTP_STATUS_NOT_IMPLEMENTED    501
    HTTP_STATUS_BAD_GATEWAY        502
    HTTP_STATUS_SERVICE_UNAVAILABLE 503
    HTTP_STATUS_GATEWAY_TIMEOUT    504
    HTTP_STATUS_UNSUPPORTED_VERSION 505
*)
  Mit Zitat antworten Zitat