您可以使用此网站:http://ipinfo.io/json。它以JSON
格式返回有关您当前互联网连接的信息。
在 Delphi 中,您需要使用IdHTTP
进行如下操作:IdHTTP1.Get('http://ipinfo.io/json')
,然后它将返回一个包含所有数据的字符串。您可以使用喜欢的JSON
解释器,或者您可以使用 lkJSON
,例如:
json := TlkJSON.ParseText(MainEstrutura.IdHTTP1.Get('http://ipinfo.io/json')) as TlkJSONobject;
str := json.Field['ip'].Value;
我希望能够帮助你。
http://ipinfo.io/ip
,它会返回纯文本格式的IP地址,而无需将其包装在需要解析的JSON中。请注意,不会改变原意。 - Remy Lebeauhttp://ipinfo.io/json
来获取有关此IP的所有信息。 - pedro.olimpio/json
是过度的,而/ip
就足够了。如果您阅读网站的文档,可以单独检索各个字段。 - Remy LebeauMyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');
在使用本服务前,请查看规则/政策:http://www.whatismyip.com/faq/automation.asp
uses JSON,IdHTTP;
function GetIP():String;
var LJsonObj : TJSONObject;
str:string;
http : TIdHttp;
begin
str:='';
http:=TIdHTTP.Create(nil);
try
str:=http.Get('http://ipinfo.io/json');
LJsonObj:= TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(str),0) as TJSONObject;
str := LJsonObj.Get('ip').JsonValue.Value;
LJsonObj.Free;
http.Free;
Except
end;
result:=str;
end;
function GetIP: String; begin with TIdHTTP.Create do try Result := http.Get('http://ipinfo.io/ip'); finally Free; end; end;
- Remy LebeauGet
替换 http.Get
) - Marus Gradinaru从记忆中,未经测试:
function GetMyHostAddress: string;
var
http: IWinHttpRequest;
begin
http := CreateOleObject('WinHttp.WinHttpRequest5.1') as IWinHttpRequest;
http.Open('GET', 'http://automation.whatismyip.com/n09230945.asp', False);
http.Send(EmptyParam);
if http.StatusCode = 200 then
Result := http.ResponseText
else
Result := '';
end;
Function GetMyIP:string;
var
xmlhttp:olevariant;
s,p:integer;
temp:string;
begin
result:=emptystr;
xmlhttp:=CreateOleObject('Microsoft.XMLHTTP');
try
xmlhttp.open('GET', 'http://www.findipinfo.com/', false);
xmlhttp.SetRequestHeader('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3');
xmlhttp.send(null);
except
exit;
end;
if(xmlhttp.status = 200) then
temp:=trim(VarToStr(xmlhttp.responseText));
xmlhttp:=Unassigned;
s:=pos('Address Is:',temp);
if s>0 then
inc(s,11)
else
exit;
temp:=copy(temp,s,30);
s:=pos('<',temp);
if s=0 then exit
else
dec(s);
result:=trim(copy(temp,1,s));
end;