1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| public class Transporter {
// URL components (should be configured variables)
private static final String HTTP = "HTTP";
private static final String HTTPS = "HTTPS";
private static final String HOSTNAME = "localhost";
private static final Integer PORT = 8443;
// secure channel key material stores (should be configured)
private static final String keystore = "C:/Users/certificat/client-keystore.jks";
private static finalStringtruststore= "C:/Users/certificat/servertruststore.jks";
private static final String keypass = "password";
private static final String trustpass = "password";
// secure channel variables
private Boolean isSecure = true;
private SSLSocketFactory sslSocketFactory = null;
public Transporter()
{
setupSocketFactory();
}
private void setupSocketFactory(){
try
{
String protocol = "TLS";
String type = "JKS";
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
String trustAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
// create and initialize an SSLContext object
SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(getKeyManagers(type, algorithm),
getTrustManagers(type, trustAlgorithm),
new SecureRandom());
// obtain the SSLSocketFactory from the SSLContext
sslSocketFactory = sslContext.getSocketFactory();
System.
out.println(" sslSocketFactory ---:"+ sslSocketFactory);
}
catch (Exception e) { e.printStackTrace();}
}
//fin methode
private KeyStore getStore(String type,String filename, String pwd) throws Exception{
System.out.println("---methode keystore getStore---");
KeyStore ks = KeyStore.getInstance(type);
InputStream istream = null;
try
{File ksfile = new File(filename);
istream = new FileInputStream(ksfile);
ks.load(istream, pwd != null? pwd.toCharArray(): null);
}
finally { if (istream != null) istream.close(); }
return ks;
}
//fin methode
private KeyManager[] getKeyManagers(String type,String algorithm) throws Exception
{
System.
out.println("---methode keyManager---");
KeyStore ks = getStore(type,
keystore, keypass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(ks,
keypass.toCharArray());
return kmf.getKeyManagers();
}
//fin methode
private TrustManager[] getTrustManagers(String type,String algorithm) throws Exception
{
KeyStore ts = getStore(type,
truststore, trustpass);
TrustManagerFactory tmf =TrustManagerFactory.getInstance(algorithm);
tmf.init(ts);
return tmf.getTrustManagers();
}
//fin methode public void send(String message)
{
System.
out.println("methode send(---");
byte[] bytes = message.toString().getBytes();
HttpURLConnection conn = null;
try
{
URL url = new URL(isSecure? HTTPS: HTTP, HOSTNAME, PORT, "/TESTE/servlett1");
conn = (HttpURLConnection)url.openConnection();
System.out.println("connexion conn");
if (isSecure)
{
HttpsURLConnection sconn = (HttpsURLConnection)conn;
sconn.setSSLSocketFactory(sslSocketFactory);
System.out.println("avant post");
}
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Length", "" + bytes.length);
// conn.addRequestProperty("Content-Type", CTYPE);
conn.addRequestProperty("Connection", "Keep-Alive");
conn.setDoOutput(true);
conn.setDoInput(true);
System.out.println("--avant connect");
conn.connect();
System.out.println("--apres connect");
// send POST data
OutputStream out = (OutputStream)conn.getOutputStream();
System.out.println("OutPutStream");
out.write(bytes);
out.flush();
out.close();
// get response code and data
System.out.println(conn.getResponseCode());
BufferedReader read = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String query = null;
while((query = read.readLine()) != null)
System.out.println(query);
}
catch(MalformedURLException e) { e.printStackTrace(); }
catch(ProtocolException e) { e.printStackTrace(); }
catch(IOException e) { e.printStackTrace(); }
finally { conn.disconnect(); }
}
} |
Partager