四行代码获取公网IP地址

uses msxml, RegularExpressions;

function GetPublicIP:string;
var
  req: IXMLHTTPRequest;
begin
  req := CoXMLHTTP.Create;
  req.open('get', 'http://city.ip138.com/ip2city.asp', False, EmptyStr, EmptyStr);
  req.send('');
  Result := TRegEx.Match(req.responseText, '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}').Value;  
end;


DelphiXE以上版本。

你可能感兴趣的:(四行代码获取公网IP地址)