Delphi获取公网IP地址函数

时间:2023-03-08 16:38:54
uses IdHTTP;
function GetPublicIP: string;
var
strIP, URL: string;
iStart, iEnd: Integer;
MyIdHTTP: TIdHTTP;
begin
Result := '';
MyIdHTTP := TIdHTTP.Create(nil);
try
try
URL := MyIdHTTP.Get('http://www.ip138.com/ip2city.asp');
except
end;
finally
MyIdHTTP.Free;
end; if Length(URL) <> then
begin
iStart := Pos('[', URL);
iEnd := Pos(']', URL);
if (iStart <> ) and (iEnd <> ) then
begin
strIP := Trim(Copy(URL, iStart + , iEnd - iStart - ));
if strIP <> '' then
Result := strIP;
end;
end;
end;
获取公网IP地址函数,需要引用IdHTTP.