当前位置: 源码素材网 » Delphi教程 » 详情页

Delphi检查判断网址是否存在并且可以打开

  •   时间:2018-10-04
  • 概述:检查网址

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检查判断网址是否存在并且可以打开”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。