- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从第三方网站获得了以下源代码,解释了如何使用 WinInet
从互联网下载文件。我对 API 不太熟悉,我查看了 WinInet
单元,但没有看到任何我需要的 API 调用。
我正在做的是添加报告文件下载进度的功能。我已经将这个过程封装在 TThread
中,并且一切正常。但是,只缺少一件事:在下载之前查找源文件的总大小。
参见下面我有评论的地方//HOW TO GET TOTAL SIZE?
这是我需要在开始下载文件之前找出文件的总大小的地方。我该怎么做呢?因为这段代码在下载完成之前似乎不知道文件的大小 - 这使得这个添加变得无关紧要。
procedure TInetThread.Execute;
const
BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File;
S: Bool;
D: Integer;
T: Integer;
procedure DoWork(const Amt: Integer);
begin
if assigned(FOnWork) then
FOnWork(Self, FSource, FDest, Amt, T);
end;
begin
S:= False;
try
try
if not DirectoryExists(ExtractFilePath(FDest)) then begin
ForceDirectories(ExtractFilePath(FDest));
end;
hSession:= InternetOpen(PChar(FAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hURL:= InternetOpenURL(hSession, PChar(FSource), nil, 0, 0, 0);
try
AssignFile(f, FDest);
Rewrite(f, 1);
T:= 0; //HOW TO GET TOTAL SIZE?
D:= 0;
DoWork(D);
repeat
InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen);
D:= D + BufferLen;
DoWork(D);
until BufferLen = 0;
CloseFile(f);
S:= True;
finally
InternetCloseHandle(hURL);
end
finally
InternetCloseHandle(hSession);
end;
except
on e: exception do begin
S:= False;
end;
end;
finally
if assigned(FOnComplete) then
FOnComplete(Self, FSource, FDest, S);
end;
end;
最佳答案
您可以使用 HEAD 方法并检查 Content-Length
来检索远程文件的文件大小
检查这两个方法
WinInet
如果要执行 HEAD 方法,则必须使用 HttpOpenRequest , HttpSendRequest和 HttpQueryInfo WinInet 函数。
uses
SysUtils,
Windows,
WinInet;
function GetWinInetError(ErrorCode:Cardinal): string;
const
winetdll = 'wininet.dll';
var
Len: Integer;
Buffer: PChar;
begin
Len := FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_ARGUMENT_ARRAY,
Pointer(GetModuleHandle(winetdll)), ErrorCode, 0, @Buffer, SizeOf(Buffer), nil);
try
while (Len > 0) and {$IFDEF UNICODE}(CharInSet(Buffer[Len - 1], [#0..#32, '.'])) {$ELSE}(Buffer[Len - 1] in [#0..#32, '.']) {$ENDIF} do Dec(Len);
SetString(Result, Buffer, Len);
finally
LocalFree(HLOCAL(Buffer));
end;
end;
procedure ParseURL(const lpszUrl: string; var Host, Resource: string);
var
lpszScheme : array[0..INTERNET_MAX_SCHEME_LENGTH - 1] of Char;
lpszHostName : array[0..INTERNET_MAX_HOST_NAME_LENGTH - 1] of Char;
lpszUserName : array[0..INTERNET_MAX_USER_NAME_LENGTH - 1] of Char;
lpszPassword : array[0..INTERNET_MAX_PASSWORD_LENGTH - 1] of Char;
lpszUrlPath : array[0..INTERNET_MAX_PATH_LENGTH - 1] of Char;
lpszExtraInfo : array[0..1024 - 1] of Char;
lpUrlComponents : TURLComponents;
begin
ZeroMemory(@lpszScheme, SizeOf(lpszScheme));
ZeroMemory(@lpszHostName, SizeOf(lpszHostName));
ZeroMemory(@lpszUserName, SizeOf(lpszUserName));
ZeroMemory(@lpszPassword, SizeOf(lpszPassword));
ZeroMemory(@lpszUrlPath, SizeOf(lpszUrlPath));
ZeroMemory(@lpszExtraInfo, SizeOf(lpszExtraInfo));
ZeroMemory(@lpUrlComponents, SizeOf(TURLComponents));
lpUrlComponents.dwStructSize := SizeOf(TURLComponents);
lpUrlComponents.lpszScheme := lpszScheme;
lpUrlComponents.dwSchemeLength := SizeOf(lpszScheme);
lpUrlComponents.lpszHostName := lpszHostName;
lpUrlComponents.dwHostNameLength := SizeOf(lpszHostName);
lpUrlComponents.lpszUserName := lpszUserName;
lpUrlComponents.dwUserNameLength := SizeOf(lpszUserName);
lpUrlComponents.lpszPassword := lpszPassword;
lpUrlComponents.dwPasswordLength := SizeOf(lpszPassword);
lpUrlComponents.lpszUrlPath := lpszUrlPath;
lpUrlComponents.dwUrlPathLength := SizeOf(lpszUrlPath);
lpUrlComponents.lpszExtraInfo := lpszExtraInfo;
lpUrlComponents.dwExtraInfoLength := SizeOf(lpszExtraInfo);
InternetCrackUrl(PChar(lpszUrl), Length(lpszUrl), ICU_DECODE or ICU_ESCAPE, lpUrlComponents);
Host := lpszHostName;
Resource := lpszUrlPath;
end;
function GetRemoteFileSize(const Url : string): Integer;
const
sUserAgent = 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101';
var
hInet : HINTERNET;
hConnect : HINTERNET;
hRequest : HINTERNET;
lpdwBufferLength: DWORD;
lpdwReserved : DWORD;
ServerName: string;
Resource: string;
ErrorCode : Cardinal;
begin
ParseURL(Url,ServerName,Resource);
Result:=0;
hInet := InternetOpen(PChar(sUserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hInet=nil then
begin
ErrorCode:=GetLastError;
raise Exception.Create(Format('InternetOpen Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
end;
try
hConnect := InternetConnect(hInet, PChar(ServerName), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
if hConnect=nil then
begin
ErrorCode:=GetLastError;
raise Exception.Create(Format('InternetConnect Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
end;
try
hRequest := HttpOpenRequest(hConnect, PChar('HEAD'), PChar(Resource), nil, nil, nil, 0, 0);
if hRequest<>nil then
begin
try
lpdwBufferLength:=SizeOf(Result);
lpdwReserved :=0;
if not HttpSendRequest(hRequest, nil, 0, nil, 0) then
begin
ErrorCode:=GetLastError;
raise Exception.Create(Format('HttpOpenRequest Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
end;
if not HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @Result, lpdwBufferLength, lpdwReserved) then
begin
Result:=0;
ErrorCode:=GetLastError;
raise Exception.Create(Format('HttpQueryInfo Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
end;
finally
InternetCloseHandle(hRequest);
end;
end
else
begin
ErrorCode:=GetLastError;
raise Exception.Create(Format('HttpOpenRequest Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
end;
finally
InternetCloseHandle(hConnect);
end;
finally
InternetCloseHandle(hInet);
end;
end;
印地
另请使用 indy 检查此代码。
function GetRemoteFilesize(const Url :string) : Integer;
var
Http: TIdHTTP;
begin
Http := TIdHTTP.Create(nil);
try
Http.Head(Url);
result:= Http.Response.ContentLength;
finally
Http.Free;
end;
end;
关于delphi - 下载前使用 WinInet 确定文件总大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9165926/
我正在寻找一个简单的函数,它能够将来自 Internet 的文本或二进制文件读入 string 多变的。 令人难以置信的是,我在网络上找不到任何内容,只有所有 WinInet 的低级描述。在 MQL
我有以下代码: #include #include #include #include #include using namespace std; int main(int argc, ch
我正在使用 wininet 连接到服务器。一切正常,上传,下载,列出命令等......因此,我有一个小错误,我不知道它来自哪里。错误是某些包含超过 100 个文件(例如图像)的服务器文件夹没有全部列出
我正在使用 WinInet 和 InternetOpenUrl 下载一个文件...正在运行。但我想监控进度,所以我尝试添加一个回调函数,但由于某种原因它从未被调用过...... 代码: void CA
我对 C++ 编程还很陌生,所以请多多包涵。我正在尝试创建一个用于教育目的的 ftp 客户端,做了一些研究并决定尝试一下 Wininet,并发现了一些在线教程: 以此为例: #include #in
我有一个将网页下载到文本文件的功能 #include #include #include #include #include #pragma comment(lib, "WinINet.li
我很好奇为什么我在使用此功能时遇到问题。我正在将网络上的 PNG 文件下载到目标路径。例如下载谷歌图片到C:盘: netDownloadData("http://www.google.com/intl
我已经为 Internet Explorer 编写了一个 BHO,它 Hook WinInet 以修改来自 IE 的一些 HTTP 请求,方法是将它们重定向到内部服务器。 当我在 IE 中打开前 3
我正在试验我的 .NET 客户端和服务器之间的缓存。在 WinInet 决定缓存结果之前,我看到一个看似随机的端点命中数。 .NET 客户端使用 HttpWebRequest 发出请求: HttpWe
我发现 WinHTTP 不可重入(1、2)。 WinINET 是可重入的吗? 我们有一个与 WinINET 同步 HTTP 的 ActiveX 控件。如果浏览器(在 javascript 中)触发一个
相关 How to send a HTTP POST Request in Delphi using WinInet api : 我如何提出发布请求并跟踪进度? 这不起作用(检查评论): proced
iam 使用 wininet 下载图像并将其保存到内存流这是我的程序 procedure DownloadToStream(const Url: string; ms: TMemoryStream);
我们使用 WinInet 和 Delphi 通过 HTTPS 进行通信。WinInet 中是否有一个函数可以返回 session 中协商的协议(protocol),即 TLS1.1、TLS 1.2 等
我有一个应用程序,它大量使用 Wininet 函数从互联网获取一些数据。有时我会收到非常奇怪的与句柄相关的错误消息: Internal error in ConnectToHost when tryi
我从第三方网站获得了以下源代码,解释了如何使用 WinInet 从互联网下载文件。我对 API 不太熟悉,我查看了 WinInet 单元,但没有看到任何我需要的 API 调用。 我正在做的是添加报告文
我在 wininet.dll 深处遇到崩溃。尝试读取 HTTP_REQUEST_HANDLE_OBJECT::ReleaseConnection 中的零内存位置时崩溃了 这是实际 DLL 中的错误,而
我正在尝试检测 ftp 服务器何时关闭我在应用程序中打开的连接。我正在使用 WinInet 功能。 我发现了一些使用 InternetSetStatusCallback 函数的示例。我实现了一个回调函
我编写了一个使用 WinInet 库的程序。该程序每天运行约 8-12 小时。首先它连接到互联网,然后它使用 FTP 下载/上传文件。之后它开始一个循环,在不同的时间间隔启动最多两个线程。两个线程都在
我正在尝试连接并确保我们设计的仪器提供的网络服务器上存在各种页面。我正在尝试使用 WinInet 命令通过 C++ Win32 执行此操作。 我很高兴我已经通过 HTTP 正确连接到网络服务器: hI
我正在尝试使用 C++ 中的 WinHTTP 获取文件的内容。该文件是一个 XML 文件,由服务器上的可执行文件生成。 用于初始化、连接甚至读取指定服务器地址上的文件的代码正在运行。 // Conne
我是一名优秀的程序员,十分优秀!