delphi中检查URL是否存在的函数
2007-07-04 14:20
281 查看
检查一个 URL 是否有效函数 | |
//可用来检测网络连接是否正确,InternetCheckConnection函数检查不准确,有些情况无法检测到,而以下CheckUrl函数则不会。 //uses wininet; function CheckUrl(url: string): Boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodelen: dword; dwcode: array[1..20] of Char; res: PChar; begin Result := false; if Pos(’http://’, LowerCase(url)) = 0 then url := ’http://’ + url; 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’); //200,302未重定位标志 if Assigned(hfile) then InternetCloseHandle(hfile); InternetCloseHandle(hsession); end; end; //方法二: function CheckUrl(url: string; TimeOut: integer = 5000): boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodelen: dword; dwcode: array[1..20] of char; res: pchar; re: integer; Err1: integer; j: integer; begin if pos(’http://’, lowercase(url)) = 0 then url := ’http://’ + url; Result := false; InternetSetOption(hSession, Internet_OPTION_CONNECT_TIMEOUT, @TimeOut, 4); hSession := InternetOpen(’Mozilla/4.0’, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); //设置超时 if assigned(hsession) then begin j := 1; while true do begin hfile := InternetOpenUrl(hsession, pchar(url), nil, 0, INTERNET_FLAG_RELOAD, 0); if hfile = nil then begin j := j + 1; Err1 := GetLastError; if j 〉 5 then break; if (Err1 〈〉 12002) or (Err1 〈〉 12152) then break; sleep(2); end else begin break; end; end; dwIndex := 0; dwCodeLen := 10; HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex); res := pchar(@dwcode); re := strtointdef(res, 404); case re of 400..450: result := false; else result := true; end; if assigned(hfile) then InternetCloseHandle(hfile); InternetCloseHandle(hsession); end; end; |
相关文章推荐
- [Delphi]检查URL是否有效的函数
- [函数]function CheckUrl(url: string): Boolean; //检查一个URL是否有效函数
- 浅谈PHP检查数组中是否存在某个值 in_array 函数
- 检查某个文件或目录是否存在的函数
- PHP下常见的检查函数(变量是否存在,文件是否存在 函数是否存在 方法是否存在)
- C++检查某个文件或目录是否存在的函数
- javascript和jquery中检查函数是否存在
- 浅谈PHP检查数组中是否存在某个值 in_array 函数
- 检查数组中是否存在某个值 php-数组函数in_array()
- PHP 检查给定的键名或索引是否存在于数组中 array_key_exists 函数
- asp下检查表中是否存在某个字段(列)函数
- [函数]function CheckUrl(url: string): Boolean; //检查一个URL是否有效函数
- 能实现检查窗口是否存在的函数
- asp实现检查目录是否存在与建立目录的函数
- asp中检查某个文件夹是否存在函数
- java服务器检查url是否存在
- php函数(检查变量、函数、键是否存在)
- 检查数组中是否存在某个值 php-数组函数in_array()
- C++ 11实现检查是否存在特定的成员函数
- ORACLE:检查一个表(TABLE)/函数(FUNCTION)/过程(PROCEDRE)是否存在