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

HttpClient用证书Https cer_firefox

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

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

HttpClient用证书Https cer_firefox_02

HttpClient用证书Https cer_https_03

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"));

}