How do you use TLS configuration with a simple code example?

Transport Layer Security (TLS) is a cryptographic protocol designed to provide secure communication over a computer network. In Java, configuring TLS allows you to ensure that your applications are secure. Here's a simple example of how to set up TLS in a Java application that uses an HTTPS connection.

import javax.net.ssl.HttpsURLConnection; import java.net.URL; public class TlsExample { public static void main(String[] args) { try { // URL of the server to connect to URL url = new URL("https://example.com"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); // Set request method connection.setRequestMethod("GET"); // Setting TLS version and security settings if needed // This can be customized as per your requirements, for example: System.setProperty("https.protocols", "TLSv1.2,TLSv1.3"); // Connect to the server and get the response int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); } catch (Exception e) { e.printStackTrace(); } } }

TLS Java secure communication Java HTTPS example Transport Layer Security