[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

How can you use Delphi to test to see if a website exists?

I have a table for maintaining a list of web addresses; there is a column for the address and a column for tracking whether or not it has been published.  I need to automate scanning the list and testing to see if each address is good.  How can I do this in Delphi 4?

Thanks!
0
sr-photo
Asked:
sr-photo
  • 4
  • 2
1 Solution
 
Mahdi78Commented:
try this

function IsURLExist(url:string):integer;
var h:tidhttp;
begin
  result:= -1;
  try
    h:=TIdHttp.Create(nil);
    try
      h.HandleRedirects := true;
      h.Head(url);
      result:=h.response.ResponseCode;
    finally
      freeandnil(h);
    end;
  except
  end;
end;
0
 
Mahdi78Commented:
This is update

function IsURLExist(url:string):boolean;
var h:tidhttp;
begin
  try
    h:=TIdHttp.Create(nil);
    try
      h.HandleRedirects := true;
      h.Head(url);
      result:=h.response.ResponseCode = 200;
    finally
      freeandnil(h);
    end;
  except
  result:= False;
  end;
end;
0
 
sr-photoAuthor Commented:
I am using Delphi 4; where do I find TIdHttp?  Or what is the Delphi 4 equivalent?  Thanks!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Mahdi78Commented:
It is Indy component
http://www.indyproject.org
0
 
Mahdi78Commented:
Or try this

uses wininet;

function CheckUrl(url:string):boolean;
var
hSession, hfile: hInternet;
dwindex,dwcodelen :dword;
dwcode:array[1..20] of char;
res : pchar;
begin
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;
0
 
sr-photoAuthor Commented:
spot on!  thanks for the extra effort, it is really appreciated!!!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now