欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA实现https单向认证

发布时间:2025/3/21 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JAVA实现https单向认证 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
//关于http 须要两个jar包 httpclient-4.0.jar httpcore-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));//密匙库的passwordtrustStore.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() );

总结

以上是生活随笔为你收集整理的JAVA实现https单向认证的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。