Make Java accept all certificates over HTTPS


sketch:

I'm trying to get Java to accept all certificates over HTTPS. This is for testing purposes. No errors were found until the certificate was obtained. However, after adding the following code before my code, I get an HTTPS hostname wrong: should be <sub.domain.com>error. The problem is that my url is that url. How can I solve this problem? Below is the code I've added to try and fix this.

        // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[]{
        new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
            }
        }
    };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }
Arun P Johny:

You also need to set a HostNameVarifier like:

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

public class TrustAllHostNameVerifier implements HostnameVerifier {

    public boolean verify(String hostname, SSLSession session) {
        return true;
    }

}

Then

 httpsConnection.setHostnameVerifier(new TrustAllHostNameVerifier ());

Related


Make Java accept all certificates over HTTPS

sketch: I'm trying to get Java to accept all certificates over HTTPS. This is for testing purposes. No errors were found until the certificate was obtained. However, after adding the following code before my code, I get an HTTPS hostname wrong: should be <sub.

Make Java accept all certificates over HTTPS

sketch: I'm trying to get Java to accept all certificates over HTTPS. This is for testing purposes. No errors were found until the certificate was obtained. However, after adding the following code before my code, I get an HTTPS hostname wrong: should be <sub.

Trust all certificates with HttpClient over HTTPS

Harrison Leigh: Recently posted a question about over HttpClientHttps ( found here ). I've made some progress, but I'm running into a new problem. As with my last question, I can't seem to find an example that works for me. Basically, I want my client to accep

Trust all certificates with HttpClient over HTTPS

Harrison Leigh: Recently posted a question about over HttpClientHttps ( found here ). I've made some progress, but I'm running into a new problem. As with my last question, I can't seem to find an example that works for me. Basically, I want my client to accep

Trust all certificates with HttpClient over HTTPS

Harrison Leigh: Recently posted a question about over HttpClientHttps ( found here ). I've made some progress, but I'm running into a new problem. As with my last question, I can't seem to find an example that works for me. Basically, I want my client to accep

Accept Java certificates

ms1013: I'm having trouble interacting with an HTTPS site via Java. My program uses a URL with an untrusted certificate every time it runs. The program must run on multiple systems. Currently, I have the following: public class A{ HostnameVerifier hv = new

Accept Java certificates

ms1013: I'm having trouble interacting with an HTTPS site via Java. My program uses a URL with an untrusted certificate every time it runs. The program must run on multiple systems. Currently, I have the following: public class A{ HostnameVerifier hv = new

Accept Java certificates

ms1013: I'm having trouble interacting with an HTTPS site via Java. My program uses a URL with an untrusted certificate every time it runs. The program must run on multiple systems. Currently, I have the following: public class A{ HostnameVerifier hv = new

Make OpenSSL accept expired certificates

Alex Moore I'm working through the source code trying to find a way to make OpenSSL always accept expired certificates. I can't find the link between outdated errors/alerts and actually checking the code. Can anyone point me in the right direction? (I'm not go

Accept HTTPS connections with self-signed certificates

Morten I'm trying to use the HttpClientlib for HTTPS connections, but the problem is that since the certificate is not signed by a recognized certificate authority (CA) , such as Verisign , GlobalSIgn , etc., which lists a set of Android trusted certificates,

svn doesn't accept https/ssl certificates

Peter CJ When bestcodes.org disappeared in September, I moved my private repository to https://subversion.assembla.com/ . It works just fine, except for the fact that every time I interact with the server I have to temporarily accept the certificate. > svn --v

Accept HTTPS connections with self-signed certificates

Morten I'm trying to use the HttpClientlib for HTTPS connections, but the problem is that since the certificate is not signed by a recognized certificate authority (CA) , such as Verisign , GlobalSIgn , etc., which lists a set of Android trusted certificates,

svn doesn't accept https/ssl certificates

Peter CJ When bestcodes.org disappeared in September, I moved my private repository to https://subversion.assembla.com/ . It works just fine, except for the fact that the certificate has to be temporarily accepted every time I interact with the server. > svn -

How to make Pidgin always accept expired certificates?

user201262 My work uses a local XMPP server (Wildfire, now called Openfire ). When using the Pidgin client, it always asks if I should accept an invalid (expired) certificate. I want Pidgin to always accept it without asking me. How can I do this without insta

How to make Pidgin always accept expired certificates?

user201262 My work uses a local XMPP server (Wildfire, now called Openfire ). When using the Pidgin client, it always asks if I should accept an invalid (expired) certificate. I want Pidgin to always accept it without asking me. How can I do this without insta

All https sites have expired certificates

Rami Daban Suddenly all https sites have expired certificates on all browsers and even formatting and reinstalling windows doesn't help! I have also tried many different connections. wood white Have you checked that the clock in your computer is set to the cor

How to make git accept self-signed certificates?

Ian Vink Using Git, is there a way to tell it to accept self-signed certificates? I'm hosting a git server with an https server, but currently the certificate is self-signed. When I first try to create the repository here: git push origin master -f I get the

How to make git accept self-signed certificates?

Ian Vink Using Git, is there a way to tell it to accept self-signed certificates? I'm hosting a git server with an https server, but currently the certificate is self-signed. When I first try to create the repository here: git push origin master -f I get the

Make Chrome accept self-signed localhost certificates

Johnson I have created a self signed SSL certificate for localhost CN. Firefox accepts this certificate after initially complaining about it, which is expected. However, even after adding the certificate to the system certificate store under "Trusted Roots", C

Make davfs2 accept wildcard certificates (webDAV)

Minos I have multiple servers sharing files via webDAV. Connections on all servers are secured using TLS and the same wildcard certificate. I have different subdomains pointing to their respective servers. But I can't get davfs2 to accept my wildcard certifica

Make Chrome accept self-signed localhost certificates

Johnson I have created a self signed SSL certificate for localhost CN. Firefox accepts this certificate after initially complaining about it, which is expected. However, even after adding the certificate to the system certificate store under "Trusted Roots", C

How to make git accept self-signed certificates?

Ian Vink Using Git, is there a way to tell it to accept self-signed certificates? I'm hosting a git server with an https server, but currently the certificate is self-signed. When I first try to create the repository here: git push origin master -f I get the

Make davfs2 accept wildcard certificates (webDAV)

Minos I have multiple servers sharing files via webDAV. Connections on all servers are secured using TLS and the same wildcard certificate. I have different subdomains pointing to their respective servers. But I can't get davfs2 to accept my wildcard certifica

Android 4.4 - HttpClient-TLS - accept all certificates

Riggett I am developing an application. Targeting Android 4.4 (API level 19) and I want to accept all certificates for HTTPS connections during the HTTP exchange. This is a clear choice since my app acts like a scanner. All the examples I've found use the fact