http client4 访问无效证书的HTTPS网站
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
?
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
?
public X509Certificate[] getAcceptedIssuers() {
return null;
}
?
};
?
StringBuffer result = new StringBuffer();
try {
HttpClient client = new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
HttpClient sslClient = new DefaultHttpClient(ccm,client.getParams());
HttpPost post=new HttpPost(Constant.HOST);
HttpResponse response = sslClient.execute(post)
status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(
entity.getContent(), HTTP.UTF_8));
String tempLine = rd.readLine();
while (tempLine != null) {?
result.append(tempLine);
tempLine = rd.readLine();
}
} catch (Exception e) {
throw new ServiceException("请求出错");
}