java https 单向认证_JAVA实现https单向认证

//关于http 须要两个jar包 httpclient-4.0.jarhttpcore-4.0.1.jar

private static final HttpClient httpClient = new DefaultHttpClient();

try {

//获得密匙库

KeyStore trustStore = KeyStore.getInstance("jks");

String keyStoreFile = "xxxxx.keystore";

String keyPwd = "xxxxxxx";

FileInputStream instream = new FileInputStream(new File(keyStoreFile));

//密匙库的password

trustStore.load(instream, keyPwd.toCharArray());

//注冊密匙库

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);

//不校验域名

socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

Scheme sch = new Scheme("https", socketFactory, 443);

httpClient.getConnectionManager().getSchemeRegistry().register(sch);

} catch (Exception e) {

e.printStackTrace();

}

//以下这段是调用代码。能够有非常多种写法。不局限于用HttpPost

HttpPost httpPost = new HttpPost( url );

StringEntity entity = new StringEntity(params);

entity.setContentEncoding("UTF-8");

httpPost.setEntity( entity );

//发送请求

HttpResponse response = httpClient.execute( httpPost );

String jsonStr = EntityUtils.toString( response.getEntity() );

原文:http://www.cnblogs.com/gccbuaa/p/7281386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值