~/ learn/ comp-348/ cards/ SSLSocket clients and HTTPS
1 of 5

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/b6476a25-0bcc-4f00-907c-a9230b2cabf6/flashcard utf-8 LF