I'm trying to automate the task of downloading a file from a https web site. I'm just starting out and using the Indy IdHTTP component to try to send a Get request to the URL. My problem is that I get the following error:
"An existing connection was forcibly closed by the remote host" when the Get method is called. I've done a considerable amount of research on the web in the process of getting to this point and have taken into account the following factors:
Setting the Linger Socket Option
Assigning a IdSSLIOHandlerSocketBase to the IdHTTP.IOHandler
Downloading the required SSL dll's and saving them into my application root directory
The relevant parts of my code are below:
It is the ReturnedPage:= https.Get(URL); line that generates the error.
..........................
......
TMyIdSSLIOHandlerSocketBas
e = class(TIdSSLIOHandlerSocke
tBase)
protected
FLinger: Boolean;
procedure DoAfterBind; override;
procedure SetLinger(Value: Boolean);
procedure SetLingerOpt;
public
property Linger: Boolean read FLinger write SetLinger;
function Clone : TIdSSLIOHandlerSocketBase;
override;
procedure StartSSL; override;
end;
........................
procedure TMainForm.Button1Click(Sen
der: TObject);
var
https: TIdHTTP;
URL: string;
ReturnedPage: string;
SSLIOHandler: TMyIdSSLIOHandlerSocketBas
e;
begin
SSLIOHandler:= TMyIdSSLIOHandlerSocketBas
e.Create;
SSLIOHandler.Linger := true;
URL := '
https://advisers.macquarie.com.au/security/login.html';
https := TIdHTTP.Create(nil);
try
https.IOHandler := SSLIOHandler;
TMyIdSSLIOHandlerSocketBas
e(https.IO
Handler).U
RIToCheck := URL;
ReturnedPage:= https.Get(URL);
MessagesMemo.Text := ReturnedPage;
finally
https.Free;
SSLIOHandler.Free;
end;
end;
{ TMyIdSSLIOHandlerSocketBas
e }
function TMyIdSSLIOHandlerSocketBas
e.Clone: TIdSSLIOHandlerSocketBase;
begin
inherited
end;
procedure TMyIdSSLIOHandlerSocketBas
e.StartSSL
;
begin
inherited
end;
procedure TMyIdSSLIOHandlerSocketBas
e.DoAfterB
ind;
begin
SetLingerOpt;
inherited;
end;
procedure TMyIdSSLIOHandlerSocketBas
e.SetLinge
r(Value: Boolean);
begin
if FLinger <> Value then
begin
FLinger := Value;
SetLingerOpt;
end;
end;
procedure TMyIdSSLIOHandlerSocketBas
e.SetLinge
rOpt;
begin
if BindingAllocated then
begin
if FLinger then
FBinding.SetSockOpt(Socket
OptionLeve
l.Socket, SocketOptionName.Linger, 1)
else
FBinding.SetSockOpt(Socket
OptionLeve
l.Socket, SocketOptionName.DontLinge
r, 1);
end;
end;
Regards,
David Compton
Start Free Trial