android https之二

[url=http://stackup.iteye.com/blog/1111547]android https之一[/url]
[url=http://stackup.iteye.com/blog/1111548]android https之二[/url]
[url=http://stackup.iteye.com/blog/1111550]android https之三[/url]

private HttpClient makeHttpsClient(String keyStorePasswd, int port) {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
String trustStorePath = System
.getProperty("javax.net.ssl.trustStore");
// File keystoreFile = new File(trustStorePath);
// 由于android权限原因,无法读取trustStorePath="//system/etc/security/cacerts.bks"文件,此处由sdcard代替
File keystoreFile = new File("/sdcard/cacerts.bks");
trustStore.load(new FileInputStream(keystoreFile), keyStorePasswd
.toCharArray());
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
socketFactory.setHostnameVerifier(new X509HostnameVerifier() {
public boolean verify(String host, SSLSession session) {
return true;
}

public void verify(String host, SSLSocket ssl)
throws IOException {
}

public void verify(String host, X509Certificate cert)
throws SSLException {
}

public void verify(String host, String[] cns,
String[] subjectAlts) throws SSLException {
}
});
Scheme sch = new Scheme("https", socketFactory, port);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
return httpClient;
} catch (KeyStoreException e) {
Log.e("xx", e.getMessage());
} catch (NoSuchAlgorithmException e) {
Log.e("xx", e.getMessage());
} catch (CertificateException e) {
Log.e("xx", e.getMessage());
} catch (KeyManagementException e) {
Log.e("xx", e.getMessage());
} catch (UnrecoverableKeyException e) {
Log.e("xx", e.getMessage());
} catch (IOException e) {
Log.e("xx", e.getMessage());
}
return null;
}

private void sendrequest() {
try {
HttpClient httpClient = makeHttpsClient("changeit", 8443);
HttpPost httpPost = makeHttpPost("https://10.167.17.187:8443");
HttpResponse response;
response = httpClient.execute(httpPost);

if (response != null) {
Log.i("xx", "" + response.getStatusLine().getStatusCode());
} else {
Log.i("xx", "NULL");
}
} catch (ClientProtocolException e) {
Log.e("xx", e.getMessage());
} catch (IOException e) {
Log.e("xx", e.getMessage());
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值