Search code examples
javassldropboxdropbox-apijava-6

How do you establish an HTTPS connection with Dropbox on Java JDK 1.6?


I'm writing an API that will provide Dropbox services. However, I'm running in to some issues when it comes to establishing an HTTPS connection with the SSL configuration provided by Dropbox.

Attempted Step 1: When I use the default configuration using the StandardHttpRequestor, I got a ClassCastException from the following:

private HttpsURLConnection prepRequest(String url, Iterable<Header> headers) throws IOException
{
    URL urlObject = new URL(url);
    HttpsURLConnection conn = (HttpsURLConnection) urlObject.openConnection(this.proxy);
}

with the following out exception

java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl 
cannot be cast to javax.net.ssl.HttpsURLConnection  at 
com.dropbox.core.http.StandardHttpRequestor.prepRequest(StandardHttpRequestor.java:150)  at 
com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:75)  at 
com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:23)  at 

Attempted Step 2: To resolve the ClassCastException I created a subclass to StandardHttpRequestor where I made the following change:

URL urlObject = new URL(null, url, new sun.net.www.protocol.https.Handler());

And I received the following DbxException$NetworkIO from:

public static HttpRequestor.Response startPostRaw(DbxRequestConfig requestConfig, String host,
                                              String path,
                                              byte[] body,
                                              /*@Nullable*/ArrayList<HttpRequestor.Header> headers) throws DbxException.NetworkIO
{
    String uri = buildUri(host, path);

    headers = addUserAgentHeader(headers, requestConfig);
    headers.add(new HttpRequestor.Header("Content-Length", Integer.toString(body.length)));

    try {
        HttpRequestor.Uploader uploader = requestConfig.httpRequestor.startPost(uri, headers);
        try {
            uploader.body.write(body);
            return uploader.finish();
        }
        finally {
            uploader.close();
        }
    }
    catch (IOException ex) {
        throw new DbxException.NetworkIO(ex);
    }
}

with the following out exception

com.dropbox.core.DbxException$NetworkIO: 
javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair  at 
com.dropbox.core.DbxRequestUtil.startPostRaw(DbxRequestUtil.java:192)  at 
com.dropbox.core.DbxRequestUtil.startPostNoAuth(DbxRequestUtil.java:164)  at 
com.dropbox.core.DbxRequestUtil.doPostNoAuth(DbxRequestUtil.java:326)  at 
com.dropbox.core.DbxWebAuthHelper.finish(DbxWebAuthHelper.java:43)  at 
com.dropbox.core.DbxWebAuthNoRedirect.finish(DbxWebAuthNoRedirect.java:86)  at 

Solution

  • Adding the new sun.net.www.protocol.https.Handler() solved the issue. Thanks Greg

        private HttpsURLConnection prepRequest(String url, Iterable<Header> headers) throws IOException
    {
        URL urlObject = new URL(null, url, new sun.net.www.protocol.https.Handler());
        HttpsURLConnection conn = (HttpsURLConnection) urlObject.openConnection(this.proxy);
    
        SSLConfig.apply(conn);
        conn.setConnectTimeout(DefaultTimeoutMillis);
        conn.setReadTimeout(DefaultTimeoutMillis);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
    
        configureConnection(conn);
    
        for (Header header : headers) {
            conn.addRequestProperty(header.key, header.value);
        }
    
        return conn;
    }