JAVA实现https单向认证

来源:互联网 发布:反域名查询 编辑:程序博客网 时间:2024/06/05 10:03
//关于http 需要两个jar包   httpclient-4.0.jarhttpcore-4.0.1.jarprivate 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();}//下面这段是调用代码,可以有很多种写法,不局限于用HttpPostHttpPost 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() );

6 2