Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send file to server using HTTPS 1

Status
Not open for further replies.

randall2nd

Programmer
May 30, 2001
112
US
I have recently been assigned to a project that was written in Delphi 5 and uses ICS(FPiette) components. The application currently sends a file to the server using FTP.

I have been tasked to try and do this using HTTPS.

The application will be migrated to Delphi 7, where I am told that I could use the Indy components to transfer the file using HTTPS.

The basic requirement is to get a file from the client machine to the server using HTTPS.

Can this be done?
Has anyone done this before?
Does anyone have any code snippets they can post/send?
Does anyone know where to start?

Any and all help apprectiated. Randall2nd
 
What is HTTPS ?

Do you mean HTTP ?

I transfer data to a server using ICS HTTP in a few applications. It is not difficult to do and it works well. But you need an application on the server to receive the data.

Andrew
 
HTTPS is supposed to be secure, you now all those times when you enter your credit card number or when you get a dialog box that says you are about enrter/leave a secure web page.

Can you give me a little more detail on how you setup the connection. Do I just need the IP adress and port. then setup a listner on the server.

What about sending data back to the client. I need to send a response back to the client stating that the data was recieved and whether it was successfully processed ot not.

How did you send your data across, did you stream the data? Randall2nd
 
I use the THTTPCLI components from F Piette's ICS.

There are two ways of using HTTP. Either use GET for smallish amounts of data or use POST for larger amounts.

In both examples the name of the THTTPCli component is 'http'.


Here is some of my code when using GET. The data I am sending is included in the URL which is in the string called DestinationURL.
Code:
  while HTTP.State <> httpReady do
    Application.ProcessMessages;

  HTTP.URL := DestinationURL;
  HTTP.Proxy := '';
  HTTP.ProxyPort := '80';
  HTTP.RcvdStream := TMemoryStream.Create;
  result := false;
  try
    try
      HTTP.Get;
      response := Format ( '%s  Recieved %d bytes', [ ResponseData, HTTP.RcvdStream.Size] );
      HTTP.RcvdStream.Seek ( 0, 0 );
      list.LoadFromStream ( HTTP.RcvdStream );
      result := true;
    except
      on E:EHTTPException do begin
        response := Format ( '%s  FAILED: %d %s', [DestinationURL,HTTP.StatusCode, HTTP.ReasonPhrase] );
      end
      else
        raise;
    end;
  finally
    HTTP.RcvdStream.Free;
  end;

Here is some of my code when using POST. The data I am sending is in the string called values.
Code:
    DataOut := TMemoryStream.Create;
    DataOut.Write ( values[1], Length(values) );
    DataOut.Seek ( 0, soFromBeginning );
    Http.SendStream := DataOut;
    Http.RcvdStream := nil;
    Http.URL := DestinationURL;
    try
      Http.Post;
    except
      ShowMessage ('POST Failed !');
      ShowMessage ('StatusCode   = ' + IntToStr(Http.StatusCode));
      ShowMessage ('ReasonPhrase = ' + Http.ReasonPhrase);
      Exit;
    end;
    DataOut.Free;
I make no claims about the quality of the code. There was quite a lot of trial and error but it seems to be reliable now.

Andrew

 
Thanks!!! :)

I really appreciate the code snippets. :D

Will try this out and tell you how it turns out. Randall2nd
 
Well I almost got everything working now.

I am having problems with posting the data.

I tells me there is some sort of Protocol missing.

Anyone done posting with Indy components? Randall2nd
 
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 &quot;.dll&quot;s for SSL to work with the Indy components:
&quot;libeay32.dll&quot; and &quot;ssleay32.dll&quot;

I downloaded them from

Do not forget to put the &quot;.dll&quot;s in the same directory as your executable.

here is the code I used:
Code:
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 &quot;EIdConnClosedGracefully&quot;
      error that is generated when the server disconnects,
      the comments in unit.procedure &quot;TIdTCPConnection.CheckForDisconnect&quot;
      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;

Thanks to all the folks that assisted me, I hope this helps others. Randall2nd
 
Hello
ja cut and past this code and after run i got error "could not load SSL Library", What can I do? (I have dll's in same directory and I have indy 10)
 
Hi there.
Why not migrate the code into Indy right now. Indy is available to Delphi 5 too.
Sooner or later you have to migrate into Indy, anyway.

There is one problem with Indy and https. I have discovered that Indy cannot connect everytime when using to get https.
Some weird error occur, and they discuss it on several forums on the Internet.

We had to install Secureblackbox ( to ensure https communication didn't fail.

(Woow, weird spelling, but you know what I mean).

KungTure-RX.jpg

//Nordlund
 
to ShivanWK (and to all): i have the same problem. How solve you this problem?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top