Open a secure client socket from the default factory
Open a secure client socket from the default factory
Answer
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); try (SSLSocket socket = (SSLSocket) factory.createSocket("books.example.com", 443)) { Writer out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.US_ASCII); out.write("GET / HTTP/1.1\r\nHost: books.example.com\r\nConnection: close\r\n\r\n"); out.flush(); }
The first two lines are the only ones that differ from a plain HTTP client. Everything after the try header — writer, explicit charset, CRLF line endings, flush — is the Module 6 idiom unchanged, because an SSLSocket is a Socket.
Harold 4e ch10 §Creating Secure Client Sockets; §Choosing the Cipher Suites