0

I'm using socketrocket to connect to a server, everything's ok with http. My question: how to connect to server using https by socketrocket

Nhut Duong
  • 612
  • 4
  • 5

1 Answers1

0

When you start the client program, the server should already be running and listening to the port, waiting for a client to request a connection. So, the first thing the client program does is to open a socket that is connected to the server running on the specified host name and port:

String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);

try (
    Socket kkSocket = new Socket(hostName, portNumber);
    PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
        new InputStreamReader(kkSocket.getInputStream()));
)

Note: For Above code to work, you have to know the server port.

But you can also access https using the below code:

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

        URL url = new URL("https://www.verisign.com/");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String line;
        while ((line = in.readLine()) != null) {
          System.out.println(line);
        }
        in.close();
      }
Kuntal-G
  • 2,970
  • 16
  • 16
  • I'm using SocketRocket library in my iOS app. I want to know how to use SocketRocket with wss://url. Thanks! – Nhut Duong Jul 21 '14 at 08:47