首页 > 代码库 > JAVA实现https单向认证
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)); //密匙库的密码 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() );
JAVA实现https单向认证
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。