How to read a text file from the Internet resource?

后端 未结 1 1287
星月不相逢
星月不相逢 2020-12-11 19:00

I would like to read a text file containing a version number from the Internet resource. Then I need to use this version number within my script.

How to do this in I

相关标签:
1条回答
  • 2020-12-11 19:38

    There are many ways how to get a file from the Internet in InnoSetup. You can use an external library like for instance InnoTools Downloader, write your own library, or use one of the Windows COM objects. In the following example I've used the WinHttpRequest COM object for file receiving.

    The DownloadFile function in this script returns True, when the WinHTTP functions doesn't raise any exception, False otherwise. The response content of the HTTP GET request to an URL, specified by the AURL parameter is then passed to a declared AResponse parameter. When the script fails the run on exception, AResponse parameter will contain the exception error message:

    [Code]
    function DownloadFile(const AURL: string; var AResponse: string): Boolean;
    var
      WinHttpRequest: Variant;
    begin
      Result := True;
      try
        WinHttpRequest := CreateOleObject('WinHttp.WinHttpRequest.5.1');
        WinHttpRequest.Open('GET', AURL, False);
        WinHttpRequest.Send;
        AResponse := WinHttpRequest.ResponseText;
      except
        Result := False;
        AResponse := GetExceptionMessage;
      end;
    end;
    
    procedure InitializeWizard;
    var
      S: string;
    begin
      if DownloadFile('http://www.example.com/versioninfo.txt', S) then
        MsgBox(S, mbInformation, MB_OK)
      else
        MsgBox(S, mbError, MB_OK)
    end;
    
    0 讨论(0)
提交回复
热议问题