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

标签 java ssl dropbox dropbox-api java-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 

最佳答案

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

关于java - 如何在 Java JDK 1.6 上与 Dropbox 建立 HTTPS 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022986/

相关文章:

java - 从代码点整数列表生成 String 对象?

java - RESTful 层次结构以及 JAX RS 类

url - SSL 是否保护 uri 中 @ 之前的部分?

node.js - 如何将颁发的 SSL 证书安装到 hyperledger rest 服务器?

git - 可以在没有远程 repo 的情况下将本地 repo 保留在保管箱上吗

ios - 类型 'NSObject -> () -> ViewController' 的值没有成员 'dbRestClient'

java消除垃圾产生

java - 获取 ArrayList 中的元素后将其删除

google-chrome - 为什么 Chrome 显示带有 SHA2 证书的 "SHA1"消息

linux - 在 Bash 脚本变量中使用命令