这里是获取下载文件名的代码,但在实际应用里效果不如迅雷的,总有些得不到文件名,或者得到的文件名不正确,请提供一个获取文件名的正确方法
procedure TForm1.Button1Click(Sender: TObject);
var lpBuffer: Array [0..4096] of Char;
hOpen: HINTERNET;
hOpenURL: HINTERNET;
hFile: Integer;
lpszFile: PChar;
dwIndex: DWORD;
dwSize: DWORD;
begin
// Open the connection handle
hOpen:=InternetOpen('Delphi Agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil,
0);
// Check handle
if Assigned(hOpen) then
begin
// Resource protection
try
// Open the URL
hOpenURL:=InternetOpenUrl(hOpen,
'http://www.censusdata.abs.gov.au/ABSNavigation/download?format=xls&collection=Census&period=2006&productlabel=Ancestry%20by%20Country%20of%20Birth%20of%20Parents&producttype=Census%20Tables&method=Place%20of%20Usual%20Residence&areacode=SSC16003',
nil, 0, INTERNET_FLAG_NEED_FILE, 0);
// Check handle
if Assigned(hOpenURL) then
begin
// Resource protection
try
// Set index
dwIndex:=0;
// Set size of buffer
dwSize:=SizeOf(lpBuffer);
// Query for the content disposition
if HttpQueryInfo(hOpenURL, HTTP_QUERY_CONTENT_DISPOSITION,
@lpBuffer, dwSize, dwIndex) then
begin
// Scan for the filename= in the string
dwIndex:=Pos('filename=', lpBuffer);
// Check for match
if (dwIndex > 0) then
begin
// Set filename string
lpszFile:=@lpBuffer[dwIndex + 8];
// Create file with desired name
hFile:=FileCreate(lpszFile);
// Check handle
if not(hFile = (-1)) then
begin
// Resource protection
try
// Read from connection and write contents
while (InternetReadFile(hOpenURL, @lpBuffer,
SizeOf(lpBuffer), dwSize) and (dwSize > 0)) do
begin
// Write to the file
FileWrite(hFile, lpBuffer, dwSize);
end;
finally
// Close the file
FileClose(hFile);
end;
end;
end;
end;
finally
// Close handle
InternetCloseHandle(hOpenURL);
end;
end;
finally
// Close handle
InternetCloseHandle(hOpen);
end;
end;