본문 바로가기
Application/Delphi Lecture

idhttp 에서 https 적용하기

by 현이빈이 2008. 8. 13.
반응형
Ok got it to work. I was trying to URL_encode the params rather than param_encode.

Here is what I have learned and done.

You will need 2 ".dll"s for SSL to work with the Indy components:
"libeay32.dll" and "ssleay32.dll"

I downloaded them from
http://www.intelicom.si/download.php?op=viewdownload&cid=1

Do not forget to put the ".dll"s in the same directory as your executable.

here is the code I used:

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;

반응형