httpclient 包的https協(xié)議使用

          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.     }  

            新建一個類訪問發(fā)送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.             //設(shè)置參數(shù)  
          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)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

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

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 遵义市| 泉州市| 嘉祥县| 乌恰县| 鹤峰县| 万全县| 万宁市| 鄂托克前旗| 察哈| 上高县| 汕尾市| 英吉沙县| 马公市| 建昌县| 南丰县| 郎溪县| 滨州市| 宿迁市| 扬州市| 建昌县| 阿克苏市| 屏东市| 盐亭县| 海阳市| 昌图县| 乐山市| 文登市| 曲周县| 格尔木市| 山阳县| 方正县| 泸西县| 巢湖市| 英德市| 广河县| 辉南县| 长沙市| 澄城县| 花莲市| 三江| 阳原县|