Вот один код. Спер с одного забугорного форума. Пример работы с SSL сертификатом:
| Код | 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 :='https://www.blahblah.com/Apps/client/page'; //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;
| |