gpt4 book ai didi

java - SSLPeerUnverifiedException(Unirest)

转载 作者:行者123 更新时间:2023-12-01 11:34:52 26 4
gpt4 key购买 nike

我刚开始接触 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;

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

非常感谢

最佳答案

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

<小时/>
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();
}
}
}

关于java - SSLPeerUnverifiedException(Unirest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30107992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com