在Delphi中,你可以使用TIdHTTP
组件从http://ipinfo.io/ip获取公网IP地址。以下是一个简单的示例:
uses
IdHTTP;
function GetPublicIP: string;
var
http: TIdHTTP;
begin
http := TIdHTTP.Create(nil);
try
Result := http.Get('http://ipinfo.io/ip');
finally
http.Free;
end;
end;
这个函数将返回一个字符串,该字符串是从ipinfo.io/ip服务获取的公网IP地址。
另外,在实际应用中,建议对以上代码进行适当的异常处理,以便在无法获取公网IP时,程序可以正常运行。