【问题标题】:Check an IP address against an online list in Inno Setup根据 Inno Setup 中的在线列表检查 IP 地址
【发布时间】:2020-06-11 17:24:59
【问题描述】:

我正在使用以下代码获取用户 IP 地址

function GetIp: string;
var
  WinHttpReq: Variant;
begin
    try
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('GET', 'http://ipinfo.io/ip', False);
      WinHttpReq.Send;
      Result := Trim(WinHttpReq.ResponseText);
    except
      Log(GetExceptionMessage);
      Result := '8.8.8.8';
    end;
end;

获取用户 IP 地址后,我需要检查该 IP 地址是否已存在于我的在线 JSON 列表中。

谢谢

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    最简单的解决方案是下载您的 JSON 文本文件并搜索您的 IP 地址。

    重用您的代码以使用 HTTP(或更好的 HTTPS)检索文档:

    function HttpGet(Url: string): string;
    var
      WinHttpReq: Variant;
    begin
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('GET', Url, False);
      WinHttpReq.Send;
      Result := Trim(WinHttpReq.ResponseText);
    end;
    

    然后你可以像这样使用它:

    var
      Ip: string;
      List: string;
    begin
      try
        Ip := HttpGet('https://ipinfo.io/ip');
        List := HttpGet('https://www.example.com/publicly/available/list.json');
    
        if Pos('["' + Ip + '"]', List) > 0 then
        begin
          Log(Format('IP %s is in the list', [Ip]));
        end
          else
        begin
          Log(Format('IP %s is not in the list', [Ip]));
        end;
      except
        Log(Format('Error testing if IP is in the list - %s', [GetExceptionMessage]));
      end;
    end;
    

    尽管您必须公开您的列表。目前无法访问您的网址,除非先登录 Google。


    如果您想正确处理 JSON,请参阅
    How to parse a JSON string in Inno Setup?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      相关资源
      最近更新 更多