参照dfw上的代码写的,下载指定的链接(URL)为指定文件(AFile),但有个问题,用代理上网的连接不上,怎么解决?
uses WinINet
function UrlGetFile(const URL, AFile: string;FPB:TProgressBar=nil): Boolean;
var
hFile, HInet: HINTERNET;
Buffer: array[0..1023] of Char;
BufRead: Cardinal;
BufSize: Cardinal;
dwIndex: LongWord;
begin
Result := False;
HInet := InternetOpen('Agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(HInet) then
try
hFile := InternetOpenUrl(HInet, PChar(URL), nil, 0, INTERNET_FLAG_RELOAD + INTERNET_FLAG_KEEP_CONNECTION, 0);
if Assigned(hFile) then
try
dwIndex := 0;
BufSize := SizeOf(Buffer);
if FPB<>nil then
begin
HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, @Buffer, BufSize, dwIndex );
FPB.Max :=StrToIntDef(Buffer, 0) div SizeOf(Buffer);
FPB.Min :=0;
FPB.Position :=0;
end;
BufSize := SizeOf(Buffer);
with TFileStream.Create(AFile, fmCreate) do
try
while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
begin
Write(Buffer, BufRead);
if FPB<>nil then FPB.Position :=FPB.Position +1;
end;
Result := True;
finally
Free;
end;
finally
InternetCloseHandle(hFile);
end;
finally
InternetCloseHandle(hinet);
if FPB<>nil then FPB.Position :=0;
end;
end;