delphi 获取本机公网IP


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses  IdHTTP,
  IdStack;

function GetExternalIP(UrlIpAddress:string): string;
var
  lHTTP  : TIdHTTP;
  lStream: TStringStream;
begin
  lHTTP   := TIdHTTP.Create(nil);
  lStream := TStringStream.Create(Result);
  try
    try
      lHTTP.Get(UrlIpAddress, lStream);
      lStream.Seek(0,0);
      Result := lStream.ReadString(lStream.Size);
    except
      Result:='';
    end;
  finally
    FreeAndNil(lHTTP);
    FreeAndNil(lStream);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text:=GetExternalIP('http://ip.hao828.com/ip.asp');

end;

end.

相关阅读 >>

Delphi获取mac地址

Delphi tstringlist的用法

Delphi 计算加班时间工时的函数

Delphi xe7中获得os平台和版本

Delphi 动画窗体

decodedatetime:将一个tdatetime变量拆分成它的日期/时间 部分

Delphi如何简单取得后缀名

Delphi 捕捉异常 try except语句 和 try finally语句用法以及区别

Delphi 限制tedit控件只能输入数字,小数点和负号

Delphi byte、pbyte、tbytes的转换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...