
✌️Oracle Java added support to TLS v1.3 in 8u261-b12 update!
Java has support to TLSv1.3 protocol for Java JDK 1.8 and well supported by JDK 11 and later, this example demonstrates how you can connect to server (http address) using SSLSocket using TLSv1.3 protocol and TLS_AES_128_GCM_SHA256 cipher.
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
/**
*
* Example of TLSv1.3 connection
* This code should work with
* Java JDK 8 11, 15
*
* Note: Make sure you are using
* the the JDK version that
* Supports TLSv1.3
*
*/
public class TSLv1_3Example {
private static final String host="code2care.org";
private static final int port=443;
private static final String TLSV1_3 = "TLSv1.3";
private static final String TLS_AES_128_GCM_SHA256 = "TLS_AES_128_GCM_SHA256";
private static SSLSocket sslSocket;
public static void main(String... args) throws Exception {
getSslSocket();
sendRequest();
readResponse();
}
public static void getSslSocket() throws IOException {
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
//Setting the TSLv1.3 protocol
sslSocket.setEnabledProtocols(new String[]{TLSV1_3});
//Setting the Cipher: TLS_AES_128_GCM_SHA256
sslSocket.setEnabledCipherSuites(new String[]{TLS_AES_128_GCM_SHA256});
//Handshake
sslSocket.startHandshake();
}
/**
* Read the sslSocket response
*
*/
public static void readResponse() {
try(BufferedReader in = new BufferedReader(
new InputStreamReader(
sslSocket.getInputStream()))) {
String responseData;
while ((responseData = in.readLine()) != null)
System.out.println(responseData);
} catch (IOException e) {
System.out.println("Error occurred while reading Socket Response...");
e.printStackTrace();
}
}
/**
*
* Send Request to sslSocket
*
* @throws IOException
*/
public static void sendRequest() throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(sslSocket.getOutputStream());
PrintWriter printWriter = new PrintWriter(new BufferedWriter(outputStreamWriter));
printWriter.println("GET /test-url HTTP/1.1\r\n");
printWriter.flush();
}
}
Output:
HTTP/1.1 200 OK
Server: code2care.org
Date: Tue, 13 Apr 2021 05:43:25 GMT
Content-Type: text/html
Content-Length: 267
Connection: close
More Posts related to Java,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- How to remove Siri from Menu Bar [macOS Big Sur] - MacOS
- How to fix Microsoft Windows 10 update error 80070020 - Microsoft
- Bootstrap Nav Menu Dropdown on hover - Bootstrap
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error - Android
- How to open a new tab in Notepad++ - NotepadPlusPlus
- Notepad++ Editor alternatives for Mac OS X - NotepadPlusPlus
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- Change Height of Android ActionBar - Android