在android RESTFUL webservice中,
在HttpClient client = new DefaultHttpClient();
- 我得到了成功回复。
但现在我的网址已更改为http为https(SSL)。 - 此网址不受信任证书。
所以我使用以下@Daniel代码。
Trusting all certificates using HttpClient over HTTPS
client = WebClientDevWrapper.getNewHttpClient();
代替DefaultClient()
- ,则会收到sessionExpiredException错误。 任何帮助??
答案 0 :(得分:3)
我希望你使用这种方法
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);
}
}
这种方法让我接受证书,并尝试增加会话时间。