简体   繁体   English

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

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

I'm writing an API that will provide Dropbox services. 我正在编写一个将提供Dropbox服务的API。 However, I'm running in to some issues when it comes to establishing an HTTPS connection with the SSL configuration provided by Dropbox. 但是,在使用Dropbox提供的SSL配置建立HTTPS连接时,我遇到了一些问题。

Attempted Step 1: When I use the default configuration using the StandardHttpRequestor, I got a ClassCastException from the following: 尝试了步骤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);
}

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: 尝试了步骤2:为解决ClassCastException,我创建了StandardHttpRequestor的子类,在其中进行了以下更改:

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

And I received the following DbxException$NetworkIO from: 我从以下位置收到了以下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);
    }
}

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 

Adding the new sun.net.www.protocol.https.Handler() solved the issue. 添加新的sun.net.www.protocol.https.Handler()解决了该问题。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM