gpt4 book ai didi

android - HttpResponse 不适用于不受信任的证书

转载 作者:行者123 更新时间:2023-11-29 22:13:14 25 4
gpt4 key购买 nike

我正在尝试从 https://....com:8455 链接获取内容,其中 “服务器的证书不受信任”。内容是:“测试”。

但在 HttpResponse response = httpClient.execute(httpPost); 之后我得到 NullPointerException。我正在使用下一个代码:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 8455));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

final String url = "https://....com:8455";
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.v("data: ", HttpHelper.requestStr(response));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

谁能帮帮我?

最佳答案

NullPointerException 源于 execute() 返回 null 的事实。也就是说,没有回应。

至于造成这种情况的原因,请搜索 SO,关于连接到自签名证书或根本不检查 CA 的问题有很多答案。

关于android - HttpResponse 不适用于不受信任的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9099692/

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