网上有好多绕过证书加载的 但是有些网站是必须要求加载证书和密钥库文件,所以这就会行不通了,不再废话,上代码。

 
  
  1. import java.io.BufferedReader; 
  2. import java.io.FileInputStream; 
  3. import java.io.InputStreamReader; 
  4. import java.net.URLDecoder; 
  5. import java.security.KeyStore; 
  6. import java.util.ArrayList; 
  7. import java.util.HashMap; 
  8. import java.util.Iterator; 
  9. import java.util.List; 
  10. import java.util.Map; 
  11.  
  12. import javax.net.ssl.KeyManagerFactory; 
  13.  
  14. import javax.net.ssl.SSLContext; 
  15. import javax.net.ssl.TrustManager; 
  16. import javax.net.ssl.TrustManagerFactory; 
  17.  
  18. import org.apache.http.HttpResponse; 
  19. import org.apache.http.NameValuePair; 
  20. import org.apache.http.client.HttpClient; 
  21. import org.apache.http.client.entity.UrlEncodedFormEntity; 
  22. import org.apache.http.client.methods.HttpPost; 
  23. import org.apache.http.conn.scheme.Scheme; 
  24. import org.apache.http.conn.ssl.SSLSocketFactory; 
  25. import org.apache.http.impl.client.DefaultHttpClient; 
  26. import org.apache.http.message.BasicNameValuePair; 
  27.  
  28. public String connect(String url1, String xml) throws Exception { 
  29.         BufferedReader in = null
  30.         String keyStore = "c:\\a.pfx"; //证书的路径,pfx格式
  31.         String trustStore = "c:\\b.jks";//密钥库文件,jks格式 
  32.         String keyPass ="111111"; //pfx文件的密码
  33.         String trustPass = "22222"; //jks文件的密码
  34.  
  35.         SSLContext sslContext = null
  36.         try { 
  37.  
  38.             KeyStore ks = KeyStore.getInstance("pkcs12"); 
  39. // 加载pfx文件            
  40. ks.load(new FileInputStream(keyStore), keyPass.toCharArray()); 
  41.             KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509"); 
  42.             kmf.init(ks, keyPass.toCharArray()); 
  43.  
  44.             KeyStore ts = KeyStore.getInstance("jks"); 
  45. //加载jks文件            
  46. ts.load(new FileInputStream(trustStore), trustPass.toCharArray()); 
  47.             TrustManager[] tm; 
  48.             TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509"); 
  49.             tmf.init(ts); 
  50.             tm = tmf.getTrustManagers(); 
  51.  
  52.             sslContext = SSLContext.getInstance("SSL"); 
  53. //初始化
  54.             sslContext.init(kmf.getKeyManagers(), tm, null); 
  55.  
  56.         } catch (Exception e) { 
  57.             e.printStackTrace(); 
  58.         } 
  59.  
  60.         String result = null
  61.         try { 
  62.  
  63.             HttpClient httpclient = new DefaultHttpClient(); 
  64.   //下面是重点
  65.             SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext); 
  66.             Scheme sch = new Scheme("https"800, socketFactory); 
  67.             httpclient.getConnectionManager().getSchemeRegistry().register(sch); 
  68.             HttpPost httpPost = null
  69.  
  70.             httpPost = new HttpPost(queryUrl); 
  71.  
  72.             // 创建名/值组列表 
  73.             List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
  74.             parameters.add(new BasicNameValuePair("Name""zhangsan")); 
  75.             parameters.add(new BasicNameValuePair("passWord""123456")); 
  76.            
  77.             // 创建UrlEncodedFormEntity对象 
  78.             UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters); 
  79.             LOGGER.info("formEntity={}", formEntiry); 
  80.             httpPost.setEntity(formEntiry); 
  81.             HttpResponse httpResponse = httpclient.execute(httpPost); 
  82.             in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); 
  83.             StringBuffer sb = new StringBuffer(""); 
  84.             String line = ""
  85.             String NL = System.getProperty("line.separator"); 
  86.             while ((line = in.readLine()) != null) { 
  87.                 sb.append(line + NL); 
  88.             } 
  89.             in.close(); 
  90.             result = sb.toString(); 
  91.  
  92.             result = URLDecoder.decode(result.toString(), "GBK"); 
  93.             LOGGER.info("result={}", result); 
  94.             return result; 
  95.         } finally { 
  96.             if (in != null) { 
  97.                 try { 
  98.                     in.close(); 
  99.                 } catch (Exception e) { 
  100.                     e.printStackTrace(); 
  101.                 } 
  102.             } 
  103.         } 
  104.  
  105.     }