httpclient 包的https協議使用

          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. public class SSLClient extends DefaultHttpClient{  
          13.     public SSLClient() throws Exception{  
          14.         super();  
          15.         SSLContext ctx = SSLContext.getInstance("TLS");  
          16.         X509TrustManager tm = new X509TrustManager() {  
          17.                 @Override  
          18.                 public void checkClientTrusted(X509Certificate[] chain,  
          19.                         String authType) throws CertificateException {  
          20.                 }  
          21.                 @Override  
          22.                 public void checkServerTrusted(X509Certificate[] chain,  
          23.                         String authType) throws CertificateException {  
          24.                 }  
          25.                 @Override  
          26.                 public X509Certificate[] getAcceptedIssuers() {  
          27.                     return null;  
          28.                 }  
          29.         };  
          30.         ctx.init(nullnew TrustManager[]{tm}, null);  
          31.         SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
          32.         ClientConnectionManager ccm = this.getConnectionManager();  
          33.         SchemeRegistry sr = ccm.getSchemeRegistry();  
          34.         sr.register(new Scheme("https"443, ssf));  
          35.     }  

            新建一個類訪問發送POST方法處理

          36. import java.util.ArrayList;  
          37. import java.util.Iterator;  
          38. import java.util.List;  
          39. import java.util.Map;  
          40. import java.util.Map.Entry;  
          41. import org.apache.http.HttpEntity;  
          42. import org.apache.http.HttpResponse;  
          43. import org.apache.http.NameValuePair;  
          44. import org.apache.http.client.HttpClient;  
          45. import org.apache.http.client.entity.UrlEncodedFormEntity;  
          46. import org.apache.http.client.methods.HttpPost;  
          47. import org.apache.http.message.BasicNameValuePair;  
          48. import org.apache.http.util.EntityUtils;  
          49. /* 
          50.  * 利用HttpClient進行post請求的工具類 
          51.  */  
          52. public class HttpClientUtil {  
          53.     public String doPost(String url,Map<String,String> map,String charset){  
          54.         HttpClient httpClient = null;  
          55.         HttpPost httpPost = null;  
          56.         String result = null;  
          57.         try{  
          58.             httpClient = new SSLClient();  
          59.             httpPost = new HttpPost(url);  
          60.             //設置參數  
          61.             List<NameValuePair> list = new ArrayList<NameValuePair>();  
          62.             Iterator iterator = map.entrySet().iterator();  
          63.             while(iterator.hasNext()){  
          64.                 Entry<String,String> elem = (Entry<String, String>) iterator.next();  
          65.                 list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));  
          66.             }  
          67.             if(list.size() > 0){  
          68.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);  
          69.                 httpPost.setEntity(entity);  
          70.             }  
          71.             HttpResponse response = httpClient.execute(httpPost);  
          72.             if(response != null){  
          73.                 HttpEntity resEntity = response.getEntity();  
          74.                 if(resEntity != null){  
          75.                     result = EntityUtils.toString(resEntity,charset);  
          76.                 }  
          77.             }  
          78.         }catch(Exception ex){  
          79.             ex.printStackTrace();  
          80.         }  
          81.         return result;  
          82.     }  
          83. }  
          84. }  



          posted on 2016-03-31 17:55 花少 閱讀(187) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2016年3月>
          282912345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 博湖县| 连南| 青神县| 清徐县| 延庆县| 民丰县| 枞阳县| 板桥市| 正镶白旗| 广安市| 德格县| 牡丹江市| 全椒县| 宁乡县| 乃东县| 九寨沟县| 开阳县| 新余市| 平泉县| 四平市| 平乡县| 潞西市| 蓬莱市| 黑山县| 弥勒县| 延寿县| 思茅市| 太仆寺旗| 台南县| 资源县| 梨树县| 得荣县| 柳河县| 弋阳县| 三原县| 佛坪县| 山西省| 西乌珠穆沁旗| 阳原县| 武安市| 茌平县|