httpclient https

1.首先你要去打开一个https: ///www.xxx.com的网站
导出一个steven.cer的证书文件
2.

httpclient https_第1张图片
 
3.

httpclient https_第2张图片
 
 
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"));
     
    }
 

你可能感兴趣的:(java)