【编者按】网学网DELPHI频道为大家收集整理了“Delphi获取公网IP地址函数“提供大家参考,希望对大家有所帮助!
通过HHTP方式连接ip138网站来得到本机的公网IP地址代码如下,很简单就是获取下源码,得到IP字符!下列函数网上收集! 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) <> 0 then begin iStart := Pos('[', URL); iEnd := Pos(']', URL); if (iStart <> 0) and (iEnd <> 0) then begin strIP := Trim(Copy(URL, iStart + 1, iEnd - iStart - 1)); if strIP <> '' then Result := strIP; end; end;end;