Creating an SSL Client Socket - Java Network

Java examples for Network:SSL

Description

Creating an SSL Client Socket

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class Main {
  public void m() throws Exception {
    try {//from  w  w  w  .j  ava  2s  .c  om
      int port = 443;
      String hostname = "hostname";
      SocketFactory socketFactory = SSLSocketFactory.getDefault();
      Socket socket = socketFactory.createSocket(hostname, port);

      // Create streams to securely send and receive data to the server
      InputStream in = socket.getInputStream();
      OutputStream out = socket.getOutputStream();

      // Read from in and write to out...

      // Close the socket
      in.close();
      out.close();
    } catch (IOException e) {
    }
  }
}
> java -Djavax.net.ssl.trustStore=truststore -Djavax.net.ssl.trustStorePassword=123456 MyApp

Related Tutorials