Sending class:
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java.net. malformedurlexception;
Import java.net. url;
Import java. Security. generalsecurityexception;
Import java. Security. cert. x509certificate;
Import javax.net. SSL. hostnameverifier;
Import javax.net. SSL. httpsurlconnection;
Import javax.net. SSL. sslcontext;
Import javax.net. SSL. sslsession;
Import javax.net. SSL. x509trustmanager;
Import com. cltc. Adapter. Ce. communicationtoken;
/**
* <P> Description: Send messages over HTTPS </P>
*/
Public class datasender
{
Private Final Static string https_transmit_location = "HTTPS transmitter ";
// Rewrite the three methods of the x509trustmanager class to trust the server certificate
Private x509trustmanager xtm = new x509trustmanager (){
Public void checkclienttrusted (x509certificate [] Chain, string authtype ){}
Public void checkservertrusted (x509certificate [] Chain, string authtype ){}
Public x509certificate [] getacceptedissuers () {return NULL ;}
};
// Trust the host
Private hostnameverifier hnv = new hostnameverifier (){
Public Boolean verify (string hostname, sslsession session) {return true ;}
};
// Construct and initialize sslcontext
Public datasender (){
Sslcontext = NULL;
Try {
Sslcontext = sslcontext. getinstance ("TLS ");
X509trustmanager [] xtmarray = new x509trustmanager [] {xtm };
Sslcontext. INIT (null, xtmarray, new java. Security. securerandom ());
} Catch (generalsecurityexception GSE ){
System. Out. println ("https: datasender: general security exception .");
GSE. getstacktrace ();
}
If (sslcontext! = NULL ){
Httpsurlconnection. setdefaultsslsocketfactory (sslcontext. getsocketfactory ());
}
Httpsurlconnection. setdefaulthostnameverifier (hnv );
}
/**
* Send messages over https
*/
Public int sendmessage (communicationtoken CT) throws malformedurlexception, ioexception
{
// System. setproperty ("javax.net. debug", "true ");
URL remotehosturl;
Httpsurlconnection = NULL;
// Establish a connection
Try
{
Remotehosturl = new URL (Ct. getremotehosturi ());
System. Out. println (Ct. getremotehosturi ());
Httpsurlconnection = (httpsurlconnection) remotehosturl. openconnection ();
}
Catch (exception _ ex)
{
System. Out. println ("unable to openconnection on URL .");
_ Ex. printstacktrace ();
}
Byte [] MSG = CT. getmessage ();
Httpsurlconnection. setfollowredirects (true );
Httpsurlconnection. setinstancefollowredirects (true );
Httpsurlconnection. setdooutput (true );
Httpsurlconnection. setdoinput (true );
Httpsurlconnection. setrequestmethod ("Post ");
Httpsurlconnection. setrequestproperty ("Content-Length", integer. tostring (msg. Length ));
Httpsurlconnection. setrequestproperty ("Content-Type", "application/X-XXXXXXXX; version = 1.0 ");
Httpsurlconnection. Connect ();
Outputstream outstream = NULL;
Try
{
Outstream = httpsurlconnection. getoutputstream ();
Outstream. Write (MSG );
Outstream. Flush ();
}
Finally
{
If (outstream! = NULL)
{
Outstream. Close ();
}
}
Int responsecode = httpsurlconnection. getresponsecode ();
System. Out. println ("https_datasender:" + responsecode );
Return responsecode;
}
}
Bytes -----------------------------------------------------------------------------------------------------------------------
Servlet reception:
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import com. cltc. Adapter. Ce. communicationtoken;
Import com. cltc. Adapter. Ce. http. datareceiver;
/**
* HTTP-SSL javaser servlet.
*/
Public class httpsslpostreceiver_v11 extends httpservlet {
Public void doget (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {
Printwriter out = res. getwriter ();
Out. println ("<HTML> ");
Out. println ("<body> ");
Out. println ("<br> you have saved med an HTTPS get on this URL. <br> ");
Out. println ("to submit a message, you must post the message to this URL <br> ");
Out. println ("</body> ");
Out. println ("}
Public void dopost (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {
System. Out. println ("receive file via HTTPS .");
Int responsefrompsc;
Byte message [] = new byte [Req. getcontentlength ()];
Inputstream thisstream = Req. getinputstream ();
For (INT bytesread = 0, offset = 0; bytesread! =-1; offset + = bytesread)
{
Bytesread = thisstream. Read (message, offset, 1000 );
}
Responsefrompsc = callhttpreceiver (Res, message );
}
Public int callhttpreceiver (httpservletresponse res, byte [] Message ){
Communicationtoken Ct = new communicationtoken ();
Int responsefrompsc;
Ct. setrnmessage (Message );
Ct. setversion (1 );
Responsefrompsc = 500;
Datareceiver. getinstance (). receivemessage (CT );
Return responsefrompsc;
}
}