• Register
0 votes

Problem :

I am trying to setup a SSL Socket connection. But i am getting below error
javax.net.ssl.sslhandshakeexception received fatal alert bad_certificate
by (6.9k points)  

1 Answer

0 votes

Solution :

1. If the server certificate is signed and valid, you will only need to open the connection as usual with the use of bellow code:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL google = new URL("https://www.google.com/");
        URLConnection yc = google.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
        String inputLine;
        while ((inputLine = in.readLine()) != null) 

2. If the server's certificate is signed but you are accessing using a different IP address/domain name than the one in the certificate, you can bypass hostname verification with this:

HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify(String urlHostName,SSLSession session) {
        return true;


3. If the certificate is not signed then you need to add it to the keystore used by the JVM 

Further Readings:


by (36.1k points)  
edited by