1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
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 = '200' then
// File exists, all ok.
result := True
else if reply = '401' then
// Not authorised. Assume page exists,
// but we can't check it.
result := True
else if reply = '404' then
// No such file.
result := False
else if reply = '500' then
// Internal server error.
result := False
else
// Shouldn't get here! It means there is
// a status code left unhandled.
result := False;
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInet);
end; |
Partager