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

时间:2016-01-26 20:10:38

标签: java ssl dropbox dropbox-api jdk1.6

我正在编写一个提供Dropbox服务的API。但是,在使用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 

1 个答案:

答案 0 :(得分:0)

添加新的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;
}