首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

http client4 访问无效证件的HTTPS网站

2013-10-29 
http client4访问无效证书的HTTPS网站X509TrustManager tm new X509TrustManager() {public void checkC

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("请求出错");

}

热点排行