获得外网IP的函数,方法有点笨,请高手指正。
use IdHTTP;
function GetIP:string;
const
p1 = ‘_blank‘;
p2 = ‘direction=left width=>‘;
Http1 = ‘http://code.helpor.net/mine/ip.php‘;
Http2 = ‘http://ip.91.com/ip/showip.asp‘;
var
s:string;
function GetHttp(sHttp:string):string;
var
IdHTTP1:TIdHTTP;
begin
try
IdHTTP1 := TIdHTTP.Create(nil);
try
Result := IdHTTP1.Get(sHttp);
finally
IdHTTP1.Free;
end;
except
end;
end;
begin
Result := ‘获取IP失败‘;
s := GetHttp(Http1);
if (s <> ‘‘) and (pos(p1, s) > 0) then
begin
s := MidBStr(s, pos(p1, s) + length(p1) + 2, 17);
s := MidBStr(s, 1, pos(‘<‘, s)-1);
Result := s;
end
else begin
s := GetHttp(Http2);
if pos(p2, s) <= 0 then exit;
s := MidbStr(s, pos(p2, s) + length(p2), 15);
s := MidBStr(s, 1, pos(‘ ‘, s)-1);
Result := s;
end;
end;