如何从 Internet 资源中读取文本文件? [英] How to read a text file from the Internet resource?

查看:21
本文介绍了如何从 Internet 资源中读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Internet 资源中读取包含版本号的文本文件.然后我需要在我的脚本中使用这个版本号.

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.

如何在 InnoSetup 中执行此操作?

How to do this in InnoSetup ?

推荐答案

在 InnoSetup 中有很多方法可以从 Internet 获取文件.您可以使用外部库,例如 InnoTools Downloader,编写您自己的库,或使用 Windows COM 对象之一.在以下示例中,我使用了 WinHttpRequest 用于文件接收的 COM 对象.

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.

此脚本中的 DownloadFile 函数返回 True,当 WinHTTP 函数没有引发任何异常时,否则返回 False.由 AURL 参数指定的对 URL 的 HTTP GET 请求的响应内容然后传递给声明的 AResponse 参数.当脚本运行异常时,AResponse 参数将包含异常错误信息:

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;

这篇关于如何从 Internet 资源中读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆