-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSSLClientExample.java
More file actions
62 lines (48 loc) · 1.58 KB
/
SSLClientExample.java
File metadata and controls
62 lines (48 loc) · 1.58 KB
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
package ssl;
//ani
import java.io.OutputStream;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/**
Visit the channel http://youtube.com/zarigatongy
*/
//Writing SSL Client in Java
public class SSLClientExample {
/*
* Where do we find the trustores ?
*/
final static String pathToStores = "/tmp/ssl/client"; // The Directory
final static String trustStoreFile = "keystore1.jks"; //The FileName
final static String passwd = "123456"; //The Password
/*
* Where is the Server and the Port
*/
final static String theServerName = "localhost";
final static int theServerPort = 12345;
/*
* Turn on SSL debugging?
*/
static boolean debug = false;
/*
* Define the client side of the test.
*/
void doClientSide() throws Exception {
SSLSocketFactory sslsf = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(theServerName,
12345);
OutputStream sslOS = sslSocket.getOutputStream();
sslOS.write("Hello SSL Server".getBytes()); // Write to the Server
sslOS.flush();
sslSocket.close();
}
public static void main(String[] args) throws Exception {
String trustFilename = pathToStores + "/"+ trustStoreFile;
System.setProperty("javax.net.ssl.trustStore", trustFilename);
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
if (debug)
System.setProperty("javax.net.debug", "all");
// java -Djavax.net.ssl.trustStore=/tmp/trustFilename -Djavax.net.ssl.trustStorePassword=123456 SSLClientExample
new SSLClientExample().doClientSide();
}
}