Indy HTTPS download HTTP Error 403 forbidden - delphi

I'm trying to download a file with the idHTTP component but its not working.
I included both OpenSSL DDLs (libeay32 & ssleay32.dll) to my project folder but its not working.
I'm using the idHTTP component with the TidSSLIOHandlerSocketOpenSSL IOHandler and SSL version SSLv3.
With this code I get an EIdHTTPProtocolException with the message "HTTP/1.1 403 Forbidden".
Why do I get this error and how can I get the download to work?
procedure TForm_Main.Button1Click(Sender: TObject);
var
fs: TFileStream;
begin
inherited;
fs := TFileStream.Create('E:\abc.html', fmCreate or fmShareDenyWrite);
try
LoadOpenSSLLibrary;
IdHTTP1.Get('https://www.testcloud.de/index.html', fs);
finally
fs.Free;
end;
end;

Related

Google Drive API v3 - error sending files in chunks

I was making an application to send backup files to the Drive, however, when I try to send the files in chunks, I am getting a 400 error in the first request.
procedure TMainForm.btnTesteClick(Sender: TObject);
const
cURL = 'URL from requset';
cFilePath = 'path of File';
var
zIdHTTP: TIdHTTP;
zParams: TIdMultiPartFormDataStream;
zIOHandler: TIdSSLIOHandlerSocketOpenSSL;
zMemFile: TFileStream;
begin
zIdHTTP:= TIdHTTP.Create();
zParams:= TIdMultiPartFormDataStream.Create();
zIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
try
zIOHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
zIOHandler.SSLOptions.Mode := sslmUnassigned;
zIOHandler.SSLOptions.VerifyMode:= [];
zIOHandler.SSLOptions.VerifyDepth:=0;
zIdHTTP.IOHandler:= zIOHandler;
zIdHTTP.HandleRedirects:= True;
try
zMemFile:= TFileStream.Create(cFilePath, fmOpenReadWrite);
zIdHTTP.Request.CustomHeaders.AddValue('Content-Length', '8388608');
zIdHTTP.Request.CustomHeaders.AddValue('Content-Range', 'bytes 0-8388607/178498359');
zParams.AddFormField('data', 'application/octet-stream', '', zMemFile);
ShowMessage(zIdHTTP.Put(cURL, zParams));
except
on E: EIdHTTPProtocolException do ShowMessage(IntToStr(zIdHTTP.ResponseCode));
on E:Exception do ShowMessage('Error: Code: '+IntToStr(zIdHTTP.ResponseCode)+' - Message: '+E.Message);
end;
finally
FreeAndNil(zMemFile);
FreeAndNil(zIdHTTP);
end;
end;
the error message that appears is as follows: "Connection reset by peer"
The request is being made with the TIDHTTP component, but I also tested it with other components and the error continues to happen.
Does anyone know how to fix this error ?
I've tried it with other components like REST, and I've also tested it with insomnia.

Cannot connect to https site with Delphi Indy control

I try to use Delphi XE3 + Indy Control(TIdhttp) to connect to a https site, but the Get method raise an exception "Error connecting with SSL. Error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv 1 alert protocol version"
Below is my code:
var
IOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
IdHTTP1.IOHandler := IOHandler;
IdHTTP1.Get(PostURL, Stream);
finally
IOHandler.Free;
end;
end;

SSL error 1409442E downloading file over HTTPS with TIdHTTP [duplicate]

This question already has an answer here:
How can we connect with a website? Getting SSL error 1409442E
(1 answer)
Closed 2 years ago.
I'm using Delphi 10.3.3. The code below used to work but now I'm getting an error when trying to download a file over HTTPS:
Error connecting with SSL error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert protocol version'
var
ms : tmemorystream;
ssl : TIdSSLIOHandlerSocketOpenSSL;
source,dest : string;
begin
source := 'https://www.myaddress.com/myfile.zip';
dest := 'c:\myfile.zip';
ms := TMemoryStream.Create;
try
if pos('https',source) > 0 then
begin
ssl := TIdSSLIOHandlerSocketOpenSSL.Create();
idh.IOHandler := ssl;
end;
idhttp1.get(source,ms);
ms.savetofile(dest);
result := 'ok';
finally
ms.Free;
end;
end;
TIdSSLIOHandlerSocketOpenSSL uses only TLS v1.0 by default and the server is rejecting that. You must explicitly allow newer TLS versions:
ssl := TIdSSLIOHandlerSocketOpenSSL.Create();
ssl.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
idh.IOHandler := ssl;
Recent versions of Indy have https support built-in (so no need to create SSL IOHandler, Indy will take care of that automatically).
Also when dealing with files, it is better to use TFilestream instead of TMemoryStream because you you will get into trouble when trying to download files that don't fit into memory.
Here is a MRE for you:
program SO60578855;
{$APPTYPE CONSOLE}
{$R *.res}
uses
idHttp,
Classes,
System.SysUtils;
var
Http : TidHttp;
Fs : TFileStream;
begin
try
Fs := TFileStream.Create('c:\temp\100mb.bin', fmcreate);
Http := TidHttp.Create(nil);
try
Http.Get('https://speed.hetzner.de/100MB.bin', Fs);
finally
Http.Free;
Fs.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.

Host not found - Trying to download a csv file using Indy in Delphi XE7

I'm trying to download a csv from the .gov.uk website, however, when I run the procedure an error is displayed: Socket Error 11001 Host not found.
procedure TFrm_Settings.btn_FetchPricesClick(Sender: TObject);
var
Url, LocalFile: String;
FileStrm: TFileStream;
Http : TIdHTTP;
begin
Url := 'http://www.gov.uk/government/uploads/system/uploads/attachment_data/file/400489/weekly_fuel_prices_csv.csv';
LocalFile := 'FuelPrices.csv';
FileStrm := TFileStream.Create(LocalFile, fmCreate);
Http:= TIDHTTP.Create(nil);
try
try
Http.Get(Url, FileStrm);
finally
FileStrm.Free;
end;
except
DeleteFile(LocalFile);
raise;
end;

Post problems with Indy TIdHTTP

I am having issues posting to Amazon's SES Service using Indy's TIdHTTP.
Here is an example of the code i am using:
procedure TMainFrm.btnAmazonSESClick(Sender: TObject);
var
SSLHandler: TIdSSLIOHandlerSocket;
HttpClient: TIdHTTP;
Params: TStringStream;
begin
SSLHandler := TIdSSLIOHandlerSocket.Create(Self);
HttpClient := TIdHTTP.Create(Self);
Params := TStringStream.create('');
try
with SSLHandler do
SSLOptions.Method := sslvSSLv3
with HttpClient do
begin
IOHandler := SSLHandler;
AllowCookies := True;
HandleRedirects := True;
HTTPOptions := [hoForceEncodeParams];
Request.ContentType := 'application/x-www-form-urlencoded';
end;
PageMemo.Text := HttpClient.Post('https://email.us-east-1.amazonaws.com?Action=VerifyEmailAddress&AWSAccessKeyId=012Some123Key46&EmailAddress=test#test%2Ecom', Params);
finally
SSLHandler.Free;
HttpClient.Free;
Params.Free;
end;
end;
Result
Under Indy 10.5.7 I get the error: HTTP/1.1 404 Not Found
Under Indy 9.0.14 I get the error: Socket Error # 11004
Debugging Trials
This same demo can successfully GET the HTML from an HTTPS web page.
If i paste the URL above into a browser it displays the expected XML result.
I would appreciate any advice on the cause.
This post is just an incomplete wild guess.
Maybe Remy might help you to correct it. With the following code I'm getting HTTP/1.1 400 Bad Request but I'm not wondering because the API reference talks about Common Query Parameters where is at least required the digital signature you'll create for the request what I don't know how to do.
I can't test this at all because I have no account there. But I think the
procedure TForm1.Button1Click(Sender: TObject);
var
HTTPClient: TIdHTTP;
Parameters: TStrings;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
HTTPClient := TIdHTTP.Create(nil);
Parameters := TStringList.Create;
try
SSLHandler.SSLOptions.Method := sslvSSLv3;
SSLHandler.SSLOptions.Mode := sslmUnassigned;
HTTPClient.IOHandler := SSLHandler;
HTTPClient.HTTPOptions := [hoForceEncodeParams];
HTTPClient.Request.ContentType := 'application/x-www-form-urlencoded';
Parameters.Add('Action=VerifyEmailAddress');
Parameters.Add('EmailAddress=test#test.com');
Parameters.Add('AWSAccessKeyId=012Some123Key46');
Parameters.Add('SignatureVersion=2');
Parameters.Add('Expires='); // ???
Parameters.Add('Signature='); // ???
PageMemo.Text := HTTPClient.Post('https://email.us-east-1.amazonaws.com', Parameters);
finally
SSLHandler.Free;
HTTPClient.Free;
Parameters.Free;
end;
end;
Basically, you need to use the right library, i.e.:
For Indy 10.5.7 use openssl-1.0.1e-i386-win32 or openssl-1.0.1e-x64_86-win64 from http://indy.fulgan.com/SSL/
You may want to download an ssl demo from: http://indy.fulgan.com/ZIP/
Regards
Jose

Resources