1. 程式人生 > >Delphi獲取公網IP地址函數

Delphi獲取公網IP地址函數

fin .get style phi del 獲取公網ip final end and

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; 獲取公網IP地址函數,需要引用IdHTTP.

Delphi獲取公網IP地址函數