unit SSL_HTTP_App;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls, SHDocVw, StdCtrls, ComCtrls, IdAntiFreezeBase, IdAntiFreeze,
IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
IdHTTP: TIdHTTP;
IdSSLIOHandlerSocket: TIdSSLIOHandlerSocket;
Edit1: TEdit;
btnPostURL: TButton;
RichEdit1: TRichEdit;
WebBrowser1: TWebBrowser;
procedure btnPostURLClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.btnPostURLClick(Sender: TObject);
var
sURL : string;
sFPR : string;
SFPRvalue : string;
sRspncFileName : string;
RspncStream : TMemoryStream;
begin
//setup idHTTP -if using the same values all the time just set in the component
//should the IP that you are connecting to
idHTTP.Host := '127.0.0.1';
//should be 80 for http and 443 for https
idHTTP.Port := 443;
//SSL ver 2 or 3
idSSLIOHandlerSocket.SSLOptions.Method := TIdSSLVersion(sslvSSLv23);
//location of the SSL certificate
idSSLIOHandlerSocket.SSLOptions.RootCertFile := 'ssl_cert.pem';
//Tells the client to use SSL
idHTTP.IOHandler := idSSLIOHandlerSocket;
//location of CGI,PERL,Servlet,... you are posting to
sURL :='[URL unfurl="true"]https://www.blahblah.com/Apps/client/page';[/URL]
//field you are posting, I just liked it seperated out
sFPR :='?MyName=';
//the value of the field, remember to encode it
sFPRvalue := idHTTP.URL.ParamsEncode('Billy Blah Bob');
//Create Stream
RspncStream := TMemoryStream.Create();
RspncStream.Clear;
RspncStream.Seek(0,soFromBeginning);
//connect
idHTTP.Connect;
(* use a try and except here to catch the "EIdConnClosedGracefully"
error that is generated when the server disconnects,
the comments in unit.procedure "TIdTCPConnection.CheckForDisconnect"
state to do this.
*)
try
//post
idHTTP.Post(sURL+SFPR+SFPRvalue,idHTTP.Request.RawHeaders,RspncStream);
except
on e : exception do
begin
Raise Exception.Create(e.ClassName+' : '+e.Message);
end;
end;
//disconnect
idHttp.Disconnect;
//display Rspnc
RspncStream.Seek(0,soFromBeginning); //reset your stream position
RichEdit1.Lines.LoadFromStream(RspncStream);
//Free Memory
RspncStream.Free;
//Display HTML
sRspncFileName := ExtractFilePath(Application.ExeName)
//tried generate a unique filename each time
sRspncFileName := sRspncFileName+IntToStr(DateTimeToFileDate(Now()))+'.html';
RichEdit1.PlainText := True;
RichEdit1.Lines.SaveToFile(sRspncFileName);
WebBrowser1.Navigate(sRspncFileName);
end;
end;