如何在 Java JDK 1.6 上与 Dropbox 建立 HTTPS 连接?

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

我正在编写一个 API 来提供 Dropbox 服务。但是,在使用 Dropbox 提供的 SSL 配置建立 HTTPS 连接时,我遇到了一些问题。

已尝试第 1 步:当我使用 StandardHttpRequestor 使用默认配置时,我从以下内容中得到了 ClassCastException:

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

有以下异常

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 

已尝试第 2 步:为了解决 ClassCastException,我创建了 StandardHttpRequestor 的子类,并在其中进行了以下更改:

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

我从以下位置收到了以下 DbxException$NetworkIO:

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);
    }
}

有以下异常

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 

添加新的 sun.net.www.protocol.https.Handler() 解决了这个问题。谢谢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;
}