×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Send file to server using HTTPS

Send file to server using HTTPS

Send file to server using HTTPS

(OP)
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

RE: Send file to server using HTTPS

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

RE: Send file to server using HTTPS

(OP)
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

RE: Send file to server using HTTPS

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.  

  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.

    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

 

RE: Send file to server using HTTPS

(OP)
Thanks!!!  :)

I really appreciate the code snippets.  :D

Will try this out and tell you how it turns out.

Randall2nd

RE: Send file to server using HTTPS

(OP)
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

RE: Send file to server using HTTPS

(OP)
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;


Thanks to all the folks that assisted me, I hope this helps others.

Randall2nd

RE: Send file to server using HTTPS

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)

RE: Send file to server using HTTPS

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 (http://www.secureblackbox.com) to ensure https communication didn't fail.

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


//Nordlund

RE: Send file to server using HTTPS

to ShivanWK (and to all): i have the same problem. How solve you this problem?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close