2005. július 15., péntek
Checking if a URL is valid
Problem/Question/Abstract:
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.
Answer:
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;
var
hSession, hfile, hRequest: hInternet;
dwindex, dwcodelen: dword;
dwcode: array[1..20] of char;
res: pchar;
begin
if pos('http://', lowercase(url)) = 0 then
url := 'http://' + url;
Result := false;
hSession := InternetOpen('InetURL:/1.0',
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if assigned(hsession) then
begin
hfile := InternetOpenUrl(
hsession,
pchar(url),
nil,
0,
INTERNET_FLAG_RELOAD,
0);
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
InternetCloseHandle(hfile);
InternetCloseHandle(hsession);
end;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése