//使用WinInet完成下载任务
...
uses
...,WinINet;
...
function TDownThread.DownloadFile(const remoteurl:string;
const store:string):Boolean;
var
Stream:TFileStream;
content:string;
NetHandle: HINTERNET;
UrlHandle:HINTERNET;
BytesRead
WORD;
Buffer:array[1..1024]of Char;
begin
Result:=false ;
NetHandle:=InternetOpen(
'htmlcopy 0.4b',
INTERNET_OPEN_TYPE_DIRECT,
nil,
nil,
0);
if Assigned(NetHandle)then begin
UrlHandle:= InternetOpenUrl(
NetHandle,
PChar(remoteurl),
nil,
0,
INTERNET_FLAG_RELOAD,
0);
if Assigned(UrlHandle)then begin
content:='';
repeat
FillChar(Buffer, SizeOf(Buffer),0);
InternetReadFile(UrlHandle,@Buffer[1],SizeOf(Buffer),BytesRead);
content:=content+copy(Buffer,1,bytesread);
until BytesRead=0;
DeleteFile(store);
if length(content)=0 then
begin result:=false; exit; end;
try
Stream:=TFileStream.Create(store,fmCreate);
except
exit;
end;
try
Stream.Write(pointer(content)^,Length(content));
Stream.Free;
result:=true;
except
Stream.Free;
end;
end;
InternetCloseHandle(UrlHandle);
end;
InternetCloseHandle(NetHandle);
end;