如何从Internet资源中读取文本文件? [英] How to read a text file from the Internet resource?
问题描述
我想从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屋!