2005. július 15., péntek

Checking if a URL is valid


You are given a list of URLs, which may or may not include the file name- eg www.msn.com instead of www.msn.com/default.asp. You want to check them automatically. The function provided does this.


This function will check the url with or without a file. The only precondition is that you must be online.

URLs can be given with or without the http:/ prefix - its adds the http:// prefix if absent- this is vital for the internetOpenUrl function which also supports FTP:// and gopher://

I am checking the return code for '200' or '302' - redirects but you may wish to check for other codes. Just modify the result := line to accomodate these codes.

uses wininet;

function CheckUrl(url: string): boolean;
  hSession, hfile, hRequest: hInternet;
  dwindex, dwcodelen: dword;
  dwcode: array[1..20] of char;
  res: pchar;
  if pos('http://', lowercase(url)) = 0 then
    url := 'http://' + url;
  Result := false;
  hSession := InternetOpen('InetURL:/1.0',
  if assigned(hsession) then
    hfile := InternetOpenUrl(
    dwIndex := 0;
    dwCodeLen := 10;
    HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE,
      @dwcode, dwcodeLen, dwIndex);
    res := pchar(@dwcode);
    result := (res = '200') or (res = '302');
    if assigned(hfile) then


Nincsenek megjegyzések:

Megjegyzés küldése