0

我不熟悉 API(官方和非官方),我正在使用一个名为JavaSnap的 API 。我一直在搞乱示例代码的一个非常基本的实现,但一直遇到错误。这是非常基本的代码:

Snapchat snapchat = Snapchat.login("xxxx", "xxxxx");

首先,我遇到了大量 ClassNotFound 错误,不得不继续下载 apache 模块(commons、httpcomponents 等)以允许程序运行,但是作为类文件,这意味着我无法一次看到我需要下载哪些模块. 因此,如果有人想告诉我我做某事有多么错误,请随意。

无论如何,现在已经清除了所有 ClassNotFound 异常(我希望)我得到以下异常:

com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLPeerUnverifiedException: Host name 'feelinsonice-hrd.appspot.com' does not match the certificate subject provided by the peer (CN=*.appspot.com, O=Google Inc, L=Mountain View, ST=California, C=US)
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:146)
    at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
    at com.habosa.javasnap.Snapchat.requestJson(Snapchat.java:953)
    at com.habosa.javasnap.Snapchat.login(Snapchat.java:160)
    at Tester.go(Tester.java:21)

据我了解,这是因为我需要启用信任所有证书,但是要做到这一点,我相信我需要将 HostNameVerifiers 与 SSLSocketFactorys 一起使用,但我不能真正开始解决这个问题,因为我只有源对于 JavaSnap API,并在堆栈中跟踪错误,我可以编辑的最新方法是:

private static HttpResponse<JsonNode> requestJson(String path, Map<String, Object> params, File file) throws UnirestException {
        MultipartBody req = prepareRequest(path, params, file);

        // Execute and return response as JSON
        HttpResponse<JsonNode> resp = req.asJson();

        // Record
        lastRequestPath = path;
        lastResponse = resp;
        lastResponseBodyClass = JsonNode.class;

        return resp;

我的问题是,我的想法真的是正确的吗?
如果我是如何实现消除此错误/信任证书的目标?如果我不是那么实际上是什么问题?

非常感谢

4

1 回答 1

1

我回答这个老问题以记住我的搜索证书错误解决方案是几个地方的组合


import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class XXX {

    private static HttpClient unsafeHttpClient;

    static {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();

            unsafeHttpClient = HttpClients.custom().setSSLContext(sslContext)
                    .setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

        } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
            e.printStackTrace();
        }
    }

    public static HttpClient getClient() {
        return unsafeHttpClient;
    }

    public static void main(String[] args) {

        try {
            HttpClient creepyClient = RestUnirestClient.getClient();
            Unirest.setHttpClient(creepyClient);

            HttpResponse<JsonNode> response = Unirest.get("https://httpbin.org/get?show_env=1").asJson();
            System.out.println(response.getBody().toString());

        } catch (UnirestException e) {
            e.printStackTrace();
        }
    }
}
于 2016-05-06T04:29:17.417 回答