url Delphi判断是否是有效的url协议
2个回答
2017-11-15 · 知道合伙人互联网行家
关注
展开全部
检查一个 URL 是否有效函数
<script type="text/javascript"> <!-- google_ad_client = "pub-9911835353109158"; google_ad_width = 200; google_ad_height = 200; google_ad_format = "200x200_as"; google_ad_type = "text"; google_ad_channel = ""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "CCCCCC"; google_color_text = "000000"; google_color_url = "008000"; //--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
//检查一个URL是否有效函数:function CheckUrl(url: string): Boolean;
//可用来检测网络连接是否正确,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;
<script type="text/javascript"> <!-- google_ad_client = "pub-9911835353109158"; google_ad_width = 200; google_ad_height = 200; google_ad_format = "200x200_as"; google_ad_type = "text"; google_ad_channel = ""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "CCCCCC"; google_color_text = "000000"; google_color_url = "008000"; //--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
//检查一个URL是否有效函数:function CheckUrl(url: string): Boolean;
//可用来检测网络连接是否正确,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;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
function IsValidURLProtocol(const URL: string): Boolean;const
Protocols: array[1..10] of string = (
// Array of valid protocols - per RFC 1738
'ftp://', 'http://', 'gopher://', 'mailto:', 'news:', 'nntp://',
'telnet://', 'wais://', 'file://', 'prospero://'
);var
I: Integer; // loops thru known protocolsbegin
// Scan array of protocols checking for a match with start of given URL
Result := False;
for I := Low(Protocols) to High(Protocols) do
if Pos(Protocols[I], SysUtils.LowerCase(URL)) = 1 then
begin
Result := True;
Exit;
end;end;
Protocols: array[1..10] of string = (
// Array of valid protocols - per RFC 1738
'ftp://', 'http://', 'gopher://', 'mailto:', 'news:', 'nntp://',
'telnet://', 'wais://', 'file://', 'prospero://'
);var
I: Integer; // loops thru known protocolsbegin
// Scan array of protocols checking for a match with start of given URL
Result := False;
for I := Low(Protocols) to High(Protocols) do
if Pos(Protocols[I], SysUtils.LowerCase(URL)) = 1 then
begin
Result := True;
Exit;
end;end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询