delphi 读取json 查询IP归属  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 读取json 查询IP归属


作者: Pawe Gowacki


在许多情况下,方便地知道运行我们的应用程序的设备的IP地址。也许您想确定谁在运行您的应用程序?通过IP地址,还可以计算设备的大概位置。如果通过路由器连接到Internet,则很有可能实现“网络地址转换”(NAT),从而使远程计算机更难与本地网络中的计算机建立直接连接。


最近,我遇到了一个非常简单且有用的REST API,该API可以获取调用它的客户端的IP地址和地理位置。通过这种方式,我们可以找出Internet在哪个IP地址“看到”我们。


如果您将HTTP GET请求发送到http://ip-api.com/json,您将收到一个JSON文档,其中包含有关您的IP地址,经度和纬度,国家/地区,城市,邮政编码,ISP提供商名称,时区和地区。


为了使该服务易于在代码中访问,我整理了一个小的Delphi“ TMyIP”类,该类提供了一个类函数“ GetInfo”,该函数使用“ TIPInfo”记录“ out”参数并返回一个布尔值,该布尔值指示该参数是否包含有效信息。实际的HTTP GET请求是使用“ System.Net.HttpClient”单元中的本机“ THTTPClient”类发出的。JSON处理使用“ TJSONTextReader”类完成。


以下是包含“ TMyIP”类的“ uMyIP”单元的完整源代码。



unit uMyIP;


interface


type

  TIPInfo = record

    status: string;

    aAs: string;

    city: string;

    country: string;

    countyCode: string;

    Isp: string;

    lat: double;

    lon: double;

    org: string;

    IPAddress: string;

    region: string;

    regionName: string;

    timezone: string;

    zip: string;

  end;


  TMyIP = class

  public

    class function GetInfo(out info: TIPInfo): boolean;

  end;


implementation


uses

  System.SysUtils,

  System.Classes,

  System.JSON.Readers,

  System.JSON.Types,

  System.Net.HttpClient;


const

  IP_API_JSON_URL = 'http://ip-api.com/json';


{ TMyIP }


class function TMyIP.GetInfo(out info: TIPInfo): boolean;

var c: THTTPClient; json: string; sr: TStringReader; jtr: TJsonTextReader;

  resp: IHTTPResponse;

begin

  Result := False;

  c := THTTPClient.Create;

  try

    try

      resp := c.Get(IP_API_JSON_URL);

      json := resp.ContentAsString;

    except

      exit;

    end;

  finally

    c.Free;

  end;


  sr := TStringReader.Create(json);

  try

    jtr := TJsonTextReader.Create(sr);

    try

      while jtr.Read do

      begin

        if jtr.TokenType = TJsonToken.PropertyName then

        begin

          if jtr.Value.ToString = 'status' then

          begin

            jtr.Read;

            info.status := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'as' then

          begin

            jtr.Read;

            info.aAs := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'city' then

          begin

            jtr.Read;

            info.city := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'country' then

          begin

            jtr.Read;

            info.country := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'countyCode' then

          begin

            jtr.Read;

            info.countyCode := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'isp' then

          begin

            jtr.Read;

            info.isp := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'lat' then

          begin

            jtr.Read;

            info.lat := jtr.Value.AsExtended;

          end


          else if jtr.Value.ToString = 'lon' then

          begin

            jtr.Read;

            info.lon := jtr.Value.AsExtended;

          end


          else if jtr.Value.ToString = 'org' then

          begin

            jtr.Read;

            info.org := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'query' then

          begin

            jtr.Read;

            info.IPAddress := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'region' then

          begin

            jtr.Read;

            info.region := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'regionName' then

          begin

            jtr.Read;

            info.regionName := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'timezone' then

          begin

            jtr.Read;

            info.timezone := jtr.Value.AsString;

          end


          else if jtr.Value.ToString = 'zip' then

          begin

            jtr.Read;

            info.zip := jtr.Value.AsString;

          end


        end;

      end

    finally

      jtr.Free;

    end;

  finally

    sr.Free;

  end;


  Result := info.status = 'success';

end;


end.



调用:

uses uMyIP;


procedure TFormMyIP.btnCheckIPClick(Sender: TObject);


  procedure AddItem(txt, det: string);

  var item: TListViewItem;

  begin

    item := lstvwInfo.Items.Add;

    item.Text := txt;

    item.Detail := det;

  end;


var info: TIPInfo; item: TListViewItem;

begin

  if TMyIP.GetInfo(info) then

  begin

    lstvwInfo.BeginUpdate;

    try

      lstvwInfo.Items.Clear;

      AddItem(info.IPAddress, 'IP address');

      AddItem(info.city, 'City');

      AddItem(info.country, 'Country');

      AddItem(info.timezone, 'Timezone');

      AddItem(info.lat.ToString, 'Latitude');

      AddItem(info.lon.ToString, 'Longitude');

    finally

      lstvwInfo.EndUpdate;

    end;

  end

  else

    ShowMessage('Failed');

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.046699047088623 seconds