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

发布于 2024-11-28 22:47:58 字数 168 浏览 0 评论 0原文

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

例如,www.whatismyip.com 显示的 IP 相同。

我怎样才能做到这一点? Winsock 不允许这样做。

I need to get my external (public) IP address from Delphi.

The same IP that is shown by www.whatismyip.com for example.

How can I do that ? Winsock doesn't allow this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-12-05 22:47:58

您可以使用此网站:http://ipinfo.io/json。它以 JSON 格式返回有关您当前互联网连接的信息。

在delphi中,您需要这样使用IdHTTPIdHTTP1.Get('http://ipinfo.io/json')
它将返回一个包含所有数据的字符串。您可以使用您喜欢的 JSON 解释器,也可以使用 lkJSON,如下例:

json := TlkJSON.ParseText(MainEstrutura.IdHTTP1.Get('http://ipinfo.io/json')) as TlkJSONobject;

str := json.Field['ip'].Value;

希望对您有所帮助。

You can use this website: http://ipinfo.io/json. It returns the information about your current internet connection in JSON format.

In delphi you need use IdHTTP this way: IdHTTP1.Get('http://ipinfo.io/json')
and it will returns a string with all the data. You can use a JSONinterpreter you like or you can use the lkJSON as the following example:

json := TlkJSON.ParseText(MainEstrutura.IdHTTP1.Get('http://ipinfo.io/json')) as TlkJSONobject;

str := json.Field['ip'].Value;

I hope help you.

傻比既视感 2024-12-05 22:47:58

我认为你不能。好吧,您可以调用一些服务来告诉您您的 IP 地址是什么(例如:http://www.whatismyip .com/ )并从响应中找出答案。但我认为您的 PC 上的任何内容都无法向外界透露您的 IP 地址。

未经测试,但我认为您可以使用 Indy 做到这一点:

MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');

请查看规则/政策:http:// /www.whatismyip.com/faq/automation.asp 在使用之前。

I don't think you can. Well, you could call some service that tells you what your IP address appears to be, ( ex: http://www.whatismyip.com/ ) and figure it out from the response. But I don't think anything on your PC will be able to tell you what your IP address looks like, to the outside world.

Untested, but I think you can do this with Indy:

MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');

Please review the rules/policy at: http://www.whatismyip.com/faq/automation.asp before using this.

李不 2024-12-05 22:47:58

这对我有用:

  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;

this works for me:

  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;
抱猫软卧 2024-12-05 22:47:58

凭记忆,未经测试:

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;

From memory, untested:

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;
苏大泽ㄣ 2024-12-05 22:47:58
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文