Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a local HTTPS URL in java?

Using the Java URL class, I can connect to an external HTTPS server (such as our production site), but using a local URL I get following exception.

"SunCertPathBuilderException: unable to find valid certification path to requested target".

How do I get a valid certification path?

EDIT: I'm not using this URL to directly create a connection, I am passing the URL to an itext PDFReader, which is then having the connection issue.

like image 567
RodeoClown Avatar asked Dec 17 '08 02:12

RodeoClown


People also ask

How do I connect to HTTPS in Java?

Therefore, if you want to create an HTTPS connection from within your applet code, simply specify HTTPS as your protocol when creating an instance of the URL class: URL url = new URL("https://[your server]");

What is a URL in Java?

Java URL. The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a resource on the World Wide Web. For example: A URL contains many information: Protocol: In this case, http is the protocol. Server name or IP Address: In this case, www.javatpoint.com is the server name.

Is there an example Java program to download HTTPS (SSL) URLs?

Sure, here’s the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can’t find the source today to give them credit, so my apologies for that.

Can you share source code for a Java HTTPS client application?

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application? Sure, here’s the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL.

What is the use of urlconnection in Java?

You will be able to write code for uploading and downloading files, web pages; sending and retrieving HTTP requests and responses; and the like. URLConnection is the superclass of all classes that represent a connection between a Java application and a URL.


1 Answers

Here was my solution that incorporates some of the ideas in this thread and peiced together with code from around the net. All I do call this function and it sets the default Trust Manager and HostName Verifier for HttpsURLConnection. This might be undesirable for some because it will effect all HttpsURLConnections but I'm just writing a simple proxy so it worked for me.

private void setTrustAllCerts() throws Exception
{
    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());
        HttpsURLConnection.setDefaultHostnameVerifier( 
            new HostnameVerifier() {
                public boolean verify(String urlHostName, SSLSession session) {
                    return true;
                }
            });
    }
    catch ( Exception e ) {
        //We can not recover from this exception.
        e.printStackTrace();
    }
}
like image 168
CornPuff Avatar answered Oct 04 '22 07:10

CornPuff