如何在Delphi中获取外部(公共)IP

13

我需要从Delphi获取我的外部(公共)IP地址。

www.whatismyip.com显示的相同IP地址。

我该怎么做? Winsock不允许这样做。


你所说的外部IP地址是指可通过互联网访问的IP地址吗?还是指不是127.0.0.1的本地网络地址? - Tremmors
可以访问互联网的地址,与www.whatismyip.com显示的相同。 - chubbyk
这个问题显示了你的计算机的实际IP地址: https://dev59.com/mXRB5IYBdhLWcg3wl4Oc - Warren P
一般情况下这是不可能的,请提出您具体的问题。 - Premature Optimization
谁说你只能有一个?不管怎样,Downvoter* 有一点是对的,你需要更详细地描述你的问题。你认为你需要外部 IP 做什么? - Marco van de Voort
5个回答

10

您可以使用此网站: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;

我希望能够帮助你。


8
如果您使用http://ipinfo.io/ip,它会返回纯文本格式的IP地址,而无需将其包装在需要解析的JSON中。请注意,不会改变原意。 - Remy Lebeau
作为这个答案中的代码,您需要使用:http://ipinfo.io/json 来获取有关此IP的所有信息。 - pedro.olimpio
2
OP没有要求检索有关IP的所有详细信息,只是要检索IP本身。因此,使用/json是过度的,而/ip就足够了。如果您阅读网站的文档,可以单独检索各个字段。 - Remy Lebeau

7
我认为你做不到。虽然你可以调用一些服务来获取你的IP地址(例如:http://www.whatismyip.com/),然后根据响应找到它。但我不认为你的电脑上有任何东西能够告诉你你的IP地址在外部看起来是什么样子。
未经测试,但我认为你可以使用Indy实现这一点:
MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');

在使用本服务前,请查看规则/政策:http://www.whatismyip.com/faq/automation.asp


12
原因是您的公共地址不一定附加在您的计算机上。很多时候,它被分配给一个外部路由器,这是一个完全不同的计算机。 - Álvaro González
是的。公共IP和默认网关适配器是两个不同的东西。 - Marco van de Voort

6
这对我有效:
  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;

4
我更喜欢使用直接报告IP地址的服务,而不需要任何额外的元数据格式。例如:function GetIP: String; begin with TIdHTTP.Create do try Result := http.Get('http://ipinfo.io/ip'); finally Free; end; end; - Remy Lebeau
@RemyLebeau 最好和简单的解决方案!(但用 Get 替换 http.Get - Marus Gradinaru

2

从记忆中,未经测试:

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;

0
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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接