1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| procedure GetInternetFile(const AServerName: string; const AURL: string; AStream: TStream);
const
BufferSize = 1024;
accept: packed array[0..1] of LPWSTR = (PChar('text/*'), nil); // PCTSTR rgpszAcceptTypes[] = {_T(text/*), NULL};
var
hSession, hHTTP, hReq : HINTERNET;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
sAppName: string;
begin
AStream.Size := 0;
sAppName := ExtractFileName(Application.ExeName);
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
// pour HTTPS : INTERNET_DEFAULT_HTTPS_PORT
hHTTP := InternetConnect(hSession, PChar(AServerName), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
try
hReq := HttpOpenRequest(hHTTP, PChar('GET'), PChar(AURL), nil, nil, @accept, 0, 1);
try
if HttpSendRequest(hReq, nil, 0, nil, 0) then
begin
BufferLen := 0;
repeat
if InternetReadFile(hReq, @Buffer, BufferSize, BufferLen) then
AStream.WriteBuffer(Buffer, BufferLen);
until BufferLen = 0;
end
else
raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hHTTP);
end;
finally
InternetCloseHandle(hSession);
end;
end; |
Partager