【问题标题】:Verify date online in Inno Setup for expiring installer在 Inno Setup 中在线验证过期安装程序的日期
【发布时间】:2018-02-02 06:34:14
【问题描述】:

安装使用 Inno Setup 开发的设置时,我有 Inno Setup 代码以显示错误消息。到期日期发生时将显示错误消息。

代码如下:

const MY_EXPIRY_DATE_STR = '20171112'; { Date format: yyyymmdd }

function InitializeSetup(): Boolean;
begin
  { If current date exceeds MY_EXPIRY_DATE_STR then return false and exit Installer. }
  result := CompareStr(GetDateTimeString('yyyymmdd', #0,#0), MY_EXPIRY_DATE_STR) <= 0;

  if not result then
    MsgBox('Due to some problem', mbError, MB_OK);
end;

现在我的问题是我想使用互联网验证日期,而不是本地系统日期。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    使用一些在线服务来检索时间(或构建您自己的服务)。

    见Free Rest API to get current time as string (timezone irrelevant)。

    确保您使用的是 HTTPS,这样就不容易绕过检查。

    以下示例使用TimeZoneDB service。

    您必须设置自己的 API 密钥(免费注册后获得)。

    const
      TimezoneDbApiKey = 'XXXXXXXXXXXX';
    
    function GetOnlineTime: string;
    var
      Url: string;
      XMLDocument: Variant;
      XMLNodeList: Variant;
      WinHttpReq: Variant;
      S: string;
      P: Integer;
    begin
      try
        { Retrieve XML from with current time in London }
        { See https://timezonedb.com/references/get-time-zone }
        WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
        Url :=
          'https://api.timezonedb.com/v2/get-time-zone?key=' + TimezoneDbApiKey +
          '&format=xml&by=zone&zone=Europe/London';
        WinHttpReq.Open('GET', Url, False);
        WinHttpReq.Send('');
        if WinHttpReq.Status <> 200 then
        begin
          Log('HTTP Error: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
        end
          else
        begin
          Log('HTTP Response: ' + WinHttpReq.ResponseText);
    
          { Parse the XML }
          XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
          XMLDocument.async := False;
          XMLDocument.loadXML(WinHttpReq.ResponseText);
    
          if XMLDocument.parseError.errorCode <> 0 then
          begin
            Log('The XML file could not be parsed. ' + XMLDocument.parseError.reason);
          end
            else
          begin
            XMLDocument.setProperty('SelectionLanguage', 'XPath');
            XMLNodeList := XMLDocument.selectNodes('/result/formatted');
            if XMLNodeList.length > 0 then
            begin
              S := Trim(XMLNodeList.item[0].text);
              { Remove the time portion }
              P := Pos(' ', S);
              if P > 0 then
              begin
                S := Copy(S, 1, P - 1);
                { Remove the dashes to get format yyyymmdd }
                StringChange(S, '-', '');
                if Length(S) <> 8 then
                begin
                  Log('Unexpected date format: ' + S);
                end
                  else
                begin
                  Result := S;
                end;
              end;
            end;
          end;
        end;
      except
        Log('Error: ' + GetExceptionMessage);
      end;
    
      if Result = '' then
      begin
        { On any problem, fallback to local time }
        Result := GetDateTimeString('yyyymmdd', #0, #0);
      end;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      相关资源
      最近更新 更多