
✌️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,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- Add HTML to Android Activity or TextView - Android
- Exception in thread main java.nio.file.NoSuchFileException - Java
- Java + Spring JDBC Template + Gradle Example - Java
- 12 August - International Youth Day celebrated worldwide - News
- How to make div or text in html unselectable using CSS - CSS
- Copy file from a remote server to current local directory system using SCP command - HowTos
- Save webpage as pdf in Google Chrome for Mac OS X - Mac-OS-X
- Maven : java.lang.ClassNotFoundException: Xmx512m - Android