1. 程式人生 > >HttpClient進行POST請求(HTTPS方式)

HttpClient進行POST請求(HTTPS方式)

原文:http://blog.csdn.net/rongyongfeikai2/article/details/41659353 

http://blog.csdn.net/lanshengsheng2012/article/details/41483929 

異常資訊如下:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

原因:伺服器的證書不被信任。一般是這樣造成的。

使用KEYTOOL工具建立證書,然後用TOMCAT啟動後,在瀏覽器開啟網站時,會出現證書不被信任的提示。當然,利用HTTPCLIENT向服務端HTTPS傳送資料時,HTTPCLIENT也會檢測服務端的證書是否被信任,不被信任就丟擲上面的異常。

解決辦法有兩種,一種是使證書被客戶端信任。另一種是使用HTTPCLIENT傳送資料時不檢測伺服器證書是否可信。

----------------------------------------------------------------------------------------------
-------------------------------------------------------------- 單向認證:保證server是真的,通道是安全的(對稱金鑰);
雙向認證:保證client和server是真的,通道是安全的(對稱金鑰);


單向認證:
1.clinet<——server
2.clinet——>server
1.client從server處拿到server的證書,通過公司的CA去驗證該證書,以確認server是真實的;
2.從server的證書中取出公鑰,對client端產生的一個金鑰加密(該金鑰即對稱金鑰)。將加密後的金鑰傳送到server端。server端用其私鑰解密出資料,即得到了對稱金鑰;
3.以後的交易都是http+該對稱金鑰加密的方式來處理;


雙向認證:
與單向認證的區別就是在1.2步驟中產生的是二分之一的對稱金鑰。
即對稱金鑰是client與server各自產生一半; ----------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------

目前,要為另一個專案提供介面,介面是用HTTP URL實現的,最初的想法是另一個專案用JQuery post進行請求。

但是,很可能另一個專案是部署在別的機器上,那麼就存在跨域問題,而JQuery的post請求是不允許跨域的。

這時,就只能夠用HttpClient包進行請求了,同時由於請求的URL是HTTPS的,為了避免需要證書,所以用一個類繼承DefaultHttpClient類,忽略校驗過程。

1.寫一個SSLClient類,繼承至HttpClient

  1. import java.security.cert.CertificateException;  
  2. import java.security.cert.X509Certificate;  
  3. import javax.net.ssl.SSLContext;  
  4. import javax.net.ssl.TrustManager;  
  5. import javax.net.ssl.X509TrustManager;  
  6. import org.apache.http.conn.ClientConnectionManager;  
  7. import org.apache.http.conn.scheme.Scheme;  
  8. import org.apache.http.conn.scheme.SchemeRegistry;  
  9. import org.apache.http.conn.ssl.SSLSocketFactory;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. //用於進行Https請求的HttpClient
  12. publicclass SSLClient extends DefaultHttpClient{  
  13.     public SSLClient() throws Exception{  
  14.         super();  
  15.         SSLContext ctx = SSLContext.getInstance("TLS");  
  16.        //SSLContext ctx = SSLContext.getInstance("SSL");
  17.         X509TrustManager tm = new X509TrustManager() {  
  18.                 @Override
  19.                 publicvoid checkClientTrusted(X509Certificate[] chain,  
  20.                         String authType) throws CertificateException {  
  21.                 }  
  22.                 @Override
  23.                 publicvoid checkServerTrusted(X509Certificate[] chain,  
  24.                         String authType) throws CertificateException {  
  25.                 }  
  26.                 @Override
  27.                 public X509Certificate[] getAcceptedIssuers() {  
  28.                     returnnull;  
  29.                 }  
  30.         };  
  31.         ctx.init(nullnew TrustManager[]{tm}, null);  
  32.         SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  33.         ClientConnectionManager ccm = this.getConnectionManager();  
  34.         SchemeRegistry sr = ccm.getSchemeRegistry();  
  35.         sr.register(new Scheme("https"443, ssf));  
  36.     }  
  37. }  

2.寫一個利用HttpClient傳送post請求的類
  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Map.Entry;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.message.BasicNameValuePair;  
  13. import org.apache.http.util.EntityUtils;  
  14. /* 
  15.  * 利用HttpClient進行post請求的工具類 
  16.  */
  17. publicclass HttpClientUtil {  
  18.     public String doPost(String url,Map<String,String> map,String charset){  
  19.         HttpClient httpClient = null;  
  20.         HttpPost httpPost = null;  
  21.         String result = null;  
  22.         try{  
  23.             httpClient = new SSLClient();  
  24.             httpPost = new HttpPost(url);  
  25.             //設定引數
  26.             List<NameValuePair> list = new ArrayList<NameValuePair>();  
  27.             Iterator iterator = map.entrySet().iterator();  
  28.             while(iterator.hasNext()){  
  29.                 Entry<String,String> elem = (Entry<String, String>) iterator.next();  
  30.                 list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));  
  31.             }  
  32.             if(list.size() > 0){  
  33.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);  
  34.                 httpPost.setEntity(entity);  
  35.             }  
  36.             HttpResponse response = httpClient.execute(httpPost);  
  37.             if(response != null){  
  38.                 HttpEntity resEntity = response.getEntity();  
  39.                 if(resEntity != null){  
  40.                     result = EntityUtils.toString(resEntity,charset);  
  41.                 }  
  42.             }  
  43.         }catch(Exception ex){  
  44.             ex.printStackTrace();  
  45.         }  
  46.         return result;  
  47.     }  
  48. }  
3.呼叫post請求的測試程式碼
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. //對介面進行測試
  4. publicclass TestMain {  
  5.     private String url = "https://192.168.1.101/";  
  6.     private String charset = "utf-8";  
  7.     private HttpClientUtil httpClientUtil = null;  
  8.     public TestMain(){  
  9.         httpClientUtil = new HttpClientUtil();  
  10.     }  
  11.     publicvoid test(){  
  12.         String httpOrgCreateTest = url + "httpOrg/create";  
  13.         Map<String,String> createMap = new HashMap<String,String>();  
  14.         createMap.put("authuser","*****");  
  15.         createMap.put("authpass","*****");  
  16.         createMap.put("orgkey","****");  
  17.         createMap.put("orgname","****");  
  18.         String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);  
  19.         System.out.println("result:"+httpOrgCreateTestRtn);  
  20.     }  
  21.     publicstaticvoid main(String[] args){  
  22.         TestMain main = new TestMain();  
  23.         main.test();  
  24.     }  
  25. }  

httpClient4.2的jar包下載路徑:http://download.csdn.net/detail/hqmryang/4582440#comment