HttpClient用证书Https cer

1、访问https的网站,下载https证书


保存证书后,是一个.cer文件。

2、利用jdk的toolkey工具,将证书转换成密钥的形式



3、代码:

public static void main(String[] args) throws ClientProtocolException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException {
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "    Mozilla/5.0 (Windows NT 6.2; rv:18.0) Gecko/20100101 Firefox/18.0");
        String PostFir = "https://www.xxx.com/";
        //获得密匙库
          KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
          FileInputStream instream = new FileInputStream(new File("d:/zzaa/steven.keystore"));
          //密匙库的密码
          trustStore.load(instream, "123456".toCharArray());
          //注册密匙库
          SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
          //不校验域名
          socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
          Scheme sch = new Scheme("https", 443, socketFactory);
          client.getConnectionManager().getSchemeRegistry().register(sch);
        HttpPost httppost1 = new HttpPost(PostFir);
        HttpResponse    response1 = client.execute(httppost1);
        HttpEntity resEntity1 = response1.getEntity();
    System.out.println(EntityUtils.toString(resEntity1,"gbk"));
     
    }



©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值