问题描述
关于如何使用WinInet下载文件的文章很多(那是我从中获得代码的地方),但是它们似乎都比较旧和/或已关闭.没有用户名和密码的下载工作正常,但是如果我使用用户名和密码保护文件夹(使用VodaHost),则在尝试下载文件时会不断收到验证错误:
There are numerous articles of how to download a file using WinInet (that's where I got the code from), but they all seem to be older and/or closed. Downloading without a username and password works fine, but if I protect the folder (Using VodaHost) with username and password I keep getting an authentication error when trying to download a file:
401未经授权...
401 Unauthorized......
如果我通过网络浏览器访问,则会弹出用户名/密码对话框,我可以接受.受保护的文件夹是:
If I access through a web browser the username/password dialog pops up and I can get in fine. The folder protected is:
http://www.mywebsite.com/downloads
我已将服务器设置为:www.mywebsite.com,并将URL设置为 http://www.mywebsite.com/下载.用户名和paswsord已经使用网络浏览器进行了验证.
I have set Server to: www.mywebsite.com and url to http://www.mywebsite.com/downloads. The username and paswsord has been validated using a web browser.
我也尝试了许多排列,并且感到有些沮丧.我能想到的唯一"是因为服务器不受保护,而是服务器上的文件夹.由于服务器可以/可以公开访问,因此无法对其进行保护.如果您需要更多信息,请告诉我.
I have also tried many permutations and am getting a bit frustrated. The 'only' thing I can think of is that it's because the server is not protected, but a folder on the server. The server cannot be protected because it is/will be publicly accessible. If you need any more info just let me know.
有人有什么主意吗?
function Download(Server, Url, User, Pass, FileName : string): boolean;
const
BUFFERSIZE = 4096;
var
hSession: HINTERNET;
hService: HINTERNET;
hHTTP: HINTERNET;
lpBuffer: array[0..BufferSize + 1] of Byte;
BufferLength: DWORD;
dwBytesRead: DWORD;
dwSizeOfRq, Reserved, dwByteToRead: DWORD;
localFile: file;
fsize: DWORD;
begin
try
try
// Downloads file at URL bypassing cache
Result := False;
// Initialize the Win32 Internet functions
hSession := InternetOpen(PChar('Empyrean'), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
// See if the session handle is valid
if hSession = nil then
Exit;
hHTTP := InternetConnect(hSession, PChar(Server), INTERNET_DEFAULT_HTTP_PORT, PChar(User), PChar(Pass), INTERNET_SERVICE_HTTP, 0, 0);
if hHTTP = nil then
Exit;
// InternetOpenUrl opens a handle to the Internet file using a URL. The flags indicate that the file will always
// be read from the Internet rather than the cache
//hService := InternetOpenUrl(hSession, pChar(url), nil, 0, INTERNET_FLAG_DONT_CACHE or INTERNET_FLAG_PRAGMA_NOCACHE or INTERNET_FLAG_RELOAD, 0);
hService := InternetOpenUrl(hSession, pChar(url), nil, 0, INTERNET_FLAG_RELOAD, 0);
// See if the session handle is valid
if hService = nil then
begin
InternetCloseHandle(hService);
Exit;
end;
HttpQueryInfo(hService, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER, @dwByteToRead, dwSizeOfRq, Reserved);
AssignFile(localFile, FileName);
{$I-}
Rewrite(localFile, 1);
{$I+}
if IOResult <> 0 then
begin
ShowMessage('Cannot create local file: ' + FileName);
InternetCloseHandle(hService);
Exit;
end;
BufferLength := BUFFERSIZE;
// These three variables will store the size of the file, the size of the HttpQueryInfo content, and the number of bytes read in
// total. Now determine the length of a file in bytes
dwByteToRead := 0;
dwSizeOfRq := 4; // BufferLength
Reserved := 0;
// get the file's size.
if not HttpQueryInfo(hService, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @dwByteToRead, dwSizeOfRq, Reserved) then
dwByteToRead := 0;
FSize := 0;
BufferLength := BUFFERSIZE;
while (BufferLength > 0) do
begin
// Read data from the hService handle
if not InternetReadFile(hService, @lpBuffer, BUFFERSIZE, BufferLength) then
Break;
if (BufferLength > 0) and (BufferLength <= BUFFERSIZE) then
BlockWrite(localFile, lpBuffer, BufferLength);
fsize := fsize + BufferLength;
// Check the size of the remaining data. If it is zero, break
if BufferLength > 0 then
Result := True;
end;
CloseFile(localFile);
Result := True;
except
end;
finally
// Close the Internet handle that the application has opened
InternetCloseHandle(hService);
InternetCloseHandle(hSession);
InternetCloseHandle(hHTTP);
end;
end;
推荐答案
感谢所有人的建议.现在,我通过在URL中包含用户名/密码来使代码起作用:
Thanks to all for the advice. I now have the code working by including the username/password in the URL:
Server := 'www.myServer.com';
URL := 'http://user:pSassword@www.myserver.com/downloads/filetodownload';
这篇关于Delphi使用带有用户名和密码的WinInet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!