Delphi WinInet带进度下载Http文件


function FileSize(SizeInBytes: dword): string;  
const
 Formats: array[0..3] of string =  (' Bytes', ' KB', ' MB', ' GB');  
 FormatSpecifier: array[Boolean] of string = ('%n', '%.2n');  
var  
 iLoop: integer;  
 TempSize: Real;  
begin  
 iLoop := -1;  
 TempSize := SizeInBytes;  
 while (iLoop <= 3) do
 begin  
 TempSize := TempSize / 1024;  
 inc(iLoop);  
 if Trunc(TempSize) = 0 then  
 begin  
 TempSize := TempSize * 1024;  
 Break;  
 end;  
 end;  
 Result := Format(FormatSpecifier[((Frac(TempSize)*10) > 1)], [TempSize]);  
 if Copy(Result, Length(Result) - 2, 3) = '.00' then  
 Result := Copy(Result, 1, Length(Result) - 3);  
 Result := Result + Formats[iLoop];  
end;  

function ExtractURLSite(FileName: string): string;  
begin  
 Result := Copy(FileName, 1, Pos('/', FileName) - 1);  
end;  

function ExtractURLPath(FileName: string): string;  
begin  
 Result := Copy(FileName, Pos('/', FileName), Length(FileName) - Pos('/', FileName) + 1);  
end;  

function Split(Input: string; Deliminator: string; Index: integer): string;  
var  
 StringLoop, StringCount: integer;  
 Buffer: string;  
begin  
 Buffer := '';  
 if Index < 1 then Exit;  
 StringCount := 0;  
 StringLoop := 1;  
 while (StringLoop <= Length(Input)) do
 begin  
 if (Copy(Input, StringLoop, Length(Deliminator)) = Deliminator) then  
 begin  
 Inc(StringLoop, Length(Deliminator) - 1);  
 Inc(StringCount);  
 if StringCount = Index then  
 begin  
 Result := Buffer;  
 Exit;  
 end  
 else
 begin  
 Buffer := '';  
 end;  
 end  
 else
 begin  
 Buffer := Buffer + Copy(Input, StringLoop, 1);  
 end;  
 Inc(StringLoop, 1);  
 end;  
 Inc(StringCount);  
 if StringCount < Index then Buffer := '';  
 Result := Buffer;  
end;  

function ThreadProc(lpParam: Pointer): DWORD; stdcall;  
const
 BufferSize = 1024;  
 Agent = 'Internet Explorer 9.9';  
var  
 Session, Connect, Resource, OpenUrl: HINTERNET;  
 Buffer: array[1..BufferSize] of Byte;  
 BufferLen: DWORD;  
 f: File;  
 FileSize, ReseRved, _SizeOf, ReadSize:DWORD;  
 UrlFile, FileName, Site, URL, Location: string;  
begin  
FileName:= 'c:\x.exe';  
Result:=0;  
UrlFile := 'http://xxxxxxxxxxxx/xxxxxxx.exe';  
Location := Split(UrlFile, '://', 2);  
URL := ExtractURLPath(Location);  
Site := ExtractURLSite(Location);  
Session := InternetOpenA(PAnsiChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);  
if Assigned(Session) then  
begin  
 try
 Connect := InternetConnectA(Session, PAnsiChar(Site), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);  
 if  Assigned(Connect) then  
 begin  
 try
 Resource := HttpOpenRequestA(Connect, 'HEAD', PChar(URL), nil, nil, nil, 0, 0);  
 if Assigned(Resource) then  
 begin  
 try
 if HttpSendRequestA(Resource, nil, 0, nil, 0) then  
 begin  
 _SizeOf := SizeOf(FileSize); //在下面api里直接用SizeOf 就说类型不一样- -!  
 ReseRved := 0;  
 FileSize := 0;  
 ReadSize := 0;  
 if (HttpQueryInfoA(Resource, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @FileSize, _SizeOf, ReseRved)) and (FileSize <> 0) then  
 begin  
 try
 OpenUrl := InternetOpenUrlA(Session, PAnsiChar(UrlFile),  nil,0,0,0);  
 if Assigned(OpenUrl) then  
 begin  
 try
 AssignFile(f, FileName);  
 Rewrite(f,1);  
 repeat  
 if InternetReadFile(OpenUrl, @Buffer,SizeOf(Buffer), BufferLen) then  
 begin  
 BlockWrite(f, Buffer, BufferLen);  
 Inc(ReadSize,BufferLen);  
 Form1.SkinGauge.Value:=ReadSize;  
 end;  
 until BufferLen = 0;  
 CloseFile(f);  
 finally  
 InternetCloseHandle(OpenUrl)  
 end  
 end;  
 finally  
 end;  
 end;  
 end;  
 finally  
 InternetCloseHandle(Resource)  
 end;  
 end;  
 finally  
 InternetCloseHandle(Connect)  
 end;  
 end;  
 finally  
 InternetCloseHandle(Session)  
 end;  
end;  
end;  

procedure TForm1.Button1Click(Sender: TObject);  
var  
 ID: DWORD;  
begin  
CreateThread(nil, 0, @ThreadProc, nil, 0, ID);  
end; 
联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678