The Delphi program described in this example is used to detect whether the URL is 404, that is, to detect whether the URL exists or whether it can be opened. Different checking methods will return different results.
The main code of the program is as follows:
unit address;interfaceuses 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:=' Checking';NMHTTP1.Head(url);end;procedure TForm1.NMHTTP1Success(Cmd: CmdType);begin case cmd of CmdHEAD: StatusBar1.SimpleText:='The URL is correct' end;end;procedure TForm1.NMHTTP1Failure(Cmd: CmdType);begin case cmd of CmdHEAD: StatusBar1.SimpleText:=' The URL is incorrect, there is no such page' end;end;procedure TForm1.NMHTTP1HostResolved(Sender: TComponent);begin StatusBar1.SimpleText:='The host name can be resolved correctly';end;procedure TForm1.NMHTTP1InvalidHost(var Handled: Boolean);begin StatusBar1.SimpleText:='The host name cannot be resolved';end; procedure TForm1.NMHTTP1AuthenticationNeeded(Sender: TObject);begin StatusBar1.SimpleText:='Authentication required';end;procedure TForm1.NMHTTP1Connect(Sender: TObject);begin StatusBar1.SimpleText:='Connect to host';end;procedure TForm1.NMHTTP1ConnectionFailed(Sender: TObject) ;begin StatusBar1.SimpleText:='Failed to connect to host';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:='Redirect to other pages';end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if Key=VK_RETURN then begin StatusBar1.simpletext:='Verifying' ; NMHTTP1.Head(Edit1.Text); end;end;end.