欢迎来到入门教程网!

Delphi

当前位置:主页 > 软件编程 > Delphi >

Delphi实现判断网址是否存在及是否可以打开的方法

来源:本站原创|时间:2020-01-10|栏目:Delphi|点击:

本例所述Delphi程序用于检测网址是否为404,也就是检测网址是否存在,或是否可以打开,针对不同的检查方法,会返回不同的结果。

程序主要代码如下:

unit address;
interface
uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Psock, NMHttp, StdCtrls, ComCtrls;
type
 TForm1 = class(TForm)
  Label1: TLabel;
  Edit1: TEdit;
  Button1: TButton;
  NMHTTP1: TNMHTTP;
  StatusBar1: TStatusBar;
  procedure Button1Click(Sender: TObject);
  procedure NMHTTP1Success(Cmd: CmdType);
  procedure NMHTTP1Failure(Cmd: CmdType);
  procedure NMHTTP1HostResolved(Sender: TComponent);
  procedure NMHTTP1InvalidHost(var Handled: Boolean);
  procedure NMHTTP1AuthenticationNeeded(Sender: TObject);
  procedure NMHTTP1Connect(Sender: TObject);
  procedure NMHTTP1ConnectionFailed(Sender: TObject);
  procedure FormActivate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure NMHTTP1Redirect(var Handled: Boolean);
  procedure Edit1KeyDown(Sender: TObject; var Key: Word;
   Shift: TShiftState);
 private
  { Private declarations }
 public
  { Public declarations }
 end;
var
 Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
 url:string;
begin
 url:=Edit1.Text;
 StatusBar1.simpletext:='正在检验';
 NMHTTP1.Head(url);
end;
procedure TForm1.NMHTTP1Success(Cmd: CmdType);
begin
 case cmd of
 CmdHEAD:
  StatusBar1.SimpleText:='网址正确'
 end;
end;
procedure TForm1.NMHTTP1Failure(Cmd: CmdType);
begin
 case cmd of
 CmdHEAD:
  StatusBar1.SimpleText:='网址不正确,没有这个页面'
 end;
end;
procedure TForm1.NMHTTP1HostResolved(Sender: TComponent);
begin
 StatusBar1.SimpleText:='正确解析主机名';
end;
procedure TForm1.NMHTTP1InvalidHost(var Handled: Boolean);
begin
 StatusBar1.SimpleText:='不能解析主机名';
end;
procedure TForm1.NMHTTP1AuthenticationNeeded(Sender: TObject);
begin
 StatusBar1.SimpleText:='需要身份验证';
end;
procedure TForm1.NMHTTP1Connect(Sender: TObject);
begin
 StatusBar1.SimpleText:='连接到主机';
end;
procedure TForm1.NMHTTP1ConnectionFailed(Sender: TObject);
begin
 StatusBar1.SimpleText:='连接主机失败';
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
 Edit1.Text:='http://www.';
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 NMHTTP1.Destroy;
end;
procedure TForm1.NMHTTP1Redirect(var Handled: Boolean);
begin
 StatusBar1.SimpleText:='重定向到其它页面';
end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
 Shift: TShiftState);
begin
 if Key=VK_RETURN then
 begin
  StatusBar1.simpletext:='正在检验';
  NMHTTP1.Head(Edit1.Text);
 end;
end;
end.

上一篇:Delphi下OpenGL2d绘图之画线的方法

栏    目:Delphi

下一篇:Delphi控件ListView的属性及使用方法详解

本文标题:Delphi实现判断网址是否存在及是否可以打开的方法

本文地址:https://www.xiuzhanwang.com/a1/Delphi/8644.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有