gpt4 book ai didi

android - Android JSON 中的 SessionExpiredException 错误

转载 作者:行者123 更新时间:2023-11-29 17:46:02 25 4
gpt4 key购买 nike

在安卓 RESTFUL 网络服务中,

  1. HttpClient client = new DefaultHttpClient(); 中 - 我得到成功响应。

  2. 但现在我的 URL 已从 http 更改为 https(SSL)。 - 并且此 url 不是受信任的证书。

所以我使用以下@Daniel 代码。

Trusting all certificates using HttpClient over HTTPS

  1. 如果我使用 client = WebClientDevWrapper.getNewHttpClient(); 而不是 DefaultClient() - 我收到 sessionExpiredException 错误。

有什么帮助吗??

最佳答案

我希望你正在使用这个方法

public void connect() throws Exception  {

HttpPost post = new HttpPost(new URI(""));
post.setEntity(new StringEntity(""));
trustAll();
HttpClient client = new DefaultHttpClient();
HttpResponse result = client.execute(post);
}

在 HttpsURLConnection 之前添加此代码,即可完成。

private void trustAll() { 
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (Exception e) {
Log.e("TAG",e);
}
}

此方法对我有效,可以接受证书并尝试增加 session 超时时间。

关于android - Android JSON 中的 SessionExpiredException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26669588/

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