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 花少 阅读(181) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

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

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜