Hello all,
I tried to download a web page (using WinInet, not using INDY components) by the following code (I found similar in StackOverflow):
function DownloadPage(const aURL, aFileName:string):boolean;
const BufferSize = 1024;
var hSession, hURL:HInternet; Buffer:array[1..BufferSize] of Byte; BufferLen:DWORD; f:file;
begin Result:=false; hSession:=InternetOpen(PChar(ExtractFileName(Application.ExeName)),
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
// Establish the secure connection with real username and password
if Copy(aURL, 1, 8) = 'https://' then
InternetConnect(hSession, PChar(aURL), INTERNET_DEFAULT_HTTPS_PORT, PChar('MyUsername'), PChar('MyPass'), INTERNET_SERVICE_HTTP, 0, 0);
try if Assigned(hSession) then begin
hURL:=InternetOpenURL(hSession, PChar(aURL), nil, 0,
INTERNET_FLAG_RELOAD, 0);
if Assigned(hURL) then
try try AssignFile(f, aFileName); Rewrite(f, 1);
repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen);
until BufferLen = 0;
Result:=True;
except Result:=false; end;
finally CloseFile(f); InternetCloseHandle(hURL); end;
end; // if Assigned(hSession)
finally InternetCloseHandle(hSession); end;
end;
It works OK with http sites, but with https sites sometimes I can not establish connection, sometimes the download goes fine. What's the matter?
Thanks in advance for eventual hints or clarifications!
Andrey