标签:
本工具是通过访问 http://city.ip138.com/ip2city.asp 并解析其返回的内容来取得本机的公网 IP 的。
源码如下。
unit PublicIPGetter; interface function GetPublicIP: string; implementation uses SysUtils, Windows, Classes, UrlMon; const cPublicIPQueryUrl: string = ‘http://city.ip138.com/ip2city.asp‘; var _FileName: string; function PublicIPFromPageText(const S: string): string; const cIPStartToken = ‘[‘; cIPEndToken = ‘]‘; var I, J: Integer; begin I := Pos(cIPStartToken, S); if I > 0 then begin J := Pos(cIPEndToken, S); if (J > 0) and (J > I) then begin Result := Copy(S, I + 1, J - I - 1); Exit; end; end; Result := ‘‘; end; function GetPublicIP: string; var L: TStringList; begin URLDownloadToFile(nil, PChar(cPublicIPQueryUrl), PChar(_FileName), 0, nil); L := TStringList.Create; L.LoadFromFile(_FileName); Result := PublicIPFromPageText(L.Text); FreeAndNil(L); DeleteFile(PChar(_FileName)); end; initialization _FileName := ExtractFilePath(ParamStr(0)) + ‘_ip_addr_tmp_.txt‘; end.
标签:
原文地址:http://www.cnblogs.com/ecofast/p/4236132.html