Delphi实现判断网址是否存在及是否可以打开的方法
Delphi实现判断网址是否存在及是否可以打开的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:本例所述Delphi程序用于检测网址是否为404,也就是检测网址是否存在,或是否可以打开,针对不同的检查方法,会返回不同的结果。程序主要代码...

本例所述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学习
热门Delphi学习
编程开发子分类