// === Get a filepath from HTTP ================================================
function GetHTTPfile(AQuery,AUser,APassword:
String;
var AResponseText:
String;
var AResponseCode:Integer;
var AMimeType:
String) :
String;
// uses idHTTP, IdSSLOpenSSL, idURI, IdException, IdStack,
// System.SysUtils, System.StrUtils, Classes
var
IdHTTP1 : TidHTTP;
LHandler : TIdSSLIOHandlerSocketOpenSSL;
lStream: TStringStream;
httpGetString, sFilename :
String;
NewGUID: TGUID;
begin
IdHTTP1 := TIdHTTP.Create(
nil);
IdHTTP1.Request.UserAgent := '
Mozilla/3.0 (compatible; Indy Library)';
lStream := TStringStream.Create(Result);
with IdHTTP1
do
begin
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(
nil);
IOHandler := LHandler;
ReadTimeout := 30000;
// OnWork := // Add here event handler
// OnWorkBegin := // Add here event handler
// = Check for authentification
if (length(AUser) < 1)
or (length(APassword) < 1)
then Request.BasicAuthentication := False
else
begin
Request.BasicAuthentication := true;
HTTPOptions := HTTPOptions +[hoInProcessAuth];
Request.Username := AUser;
Request.Password := APassword;
end;
httpGetString := TIdURI.URLDecode(AQuery);
httpGetString := TIdURI.URLEncode(httpGetString);
try
WriteLog(1,'
HTTP','
URL=' +httpGetString);
IdHTTP1.Get(TIdURI.URLEncode(httpGetString), lStream);
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
AMimeType := IdHTTP1.Response.ContentType;
lStream.Position := 0;
CreateGUID(NewGUID); sFilename := GUIDToString(NewGUID);
sFilename :=
// -> Put here estimated temporary filepath
lStream.SaveToFile(sFilename);
Result := sFilename;
except
on E: EIdHTTPProtocolException
do
begin
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
ShowMessage('
[ERROR] ' +'
Indy raised a protocol error!' +'
| '
+'
HTTP status code: ' + IntToStr(E.ErrorCode) +'
| '
+'
Error message' + E.
Message);
end;
on E: EIdConnClosedGracefully
do
begin
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
ShowMessage('
[ERROR] ' +'
Indy reports, that connection was closed gracefully!');
end;
on E: EIdSocketError
do
begin
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
ShowMessage('
[ERROR] ' +'
Indy raised a socket error!' +'
| '
+'
Error code: ' + IntToStr(E.LastError) +'
| '
+'
Error message' + E.
Message);
end;
on E: EIdException
do
begin
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
ShowMessage('
[ERROR] ' +'
Indy raised an exception!' +'
| '
+'
Exception class: ' + E.ClassName +'
| '
+'
Error message: ' + E.
Message);
end;
on E:
Exception do
begin
AResponseCode := IdHTTP1.ResponseCode;
AResponseText := IdHTTP1.ResponseText;
ShowMessage('
[ERROR] ' +'
A non-Indy related exception has been raised!');
end;
end;
end;
{httpclient}
LHandler.Free;
IdHTTP1.Disconnect;
IdHTTP1.Free;
FreeAndNil(lStream);
end;