
✌️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
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Change Title text for Android Activity using java code - Android
- How to make a div tag clickable - Html
- Skip Test Maven while creating package command - Java
- Windows 10 now has a new enhanced Calculator with a new icon - News
- SharePoint Server 2016 error Microsoft Office Online Server 2016 cant be installed on the same machine as a Microsoft SharePoint Server product - SharePoint
- How to make macOS Terminal window Transparent (or Opaque) - MacOS
- Check SSH/OpenSSH version Command - Linux
- List of All Apple Silicon ARM Based M1/M2 Mac [updated Oct 2022) - MacOS