本例所述Delphi程式用來偵測網址是否為404,也就是偵測網址是否存在,或是否可以打開,針對不同的檢查方法,會傳回不同的結果。
程式主要程式碼如下:
unit address;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Psock, NMHttp, StdCtrls, ComCtrls;type TForm1 = class(TForm) Label1: TLabel; Edit1: 105; ; StatusBar1: TStatusBar; procedure Button1Click(Sender: TObject); procedure NMHTTP1Success(Cmd: CmdType); procedure NMHTTP1Failure(Cmd: CmdType); procedure NMHTTP1HostResolved(Sender: TComponlean) procost NMHTTP1AuthenticationNeeded(Sender: TObject); procedure NMHTTP1Connect(Sender: TObject); procedure NMHTTP1ConnectionFailed(Sender: TObject); procedure FormActivate(Sender: ToseObject); procedure FormClose(Soverd); Handled: Boolean); procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}proc 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:='需要驗證);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 Key;end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key Keydss; );begin if Key=VK_RETURN then begin StatusBar1.simpletext:='正在檢驗'; NMHTTP1.Head(Edit1.Text); end;end;end.