HttpClientUtil

  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStream;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.security.KeyManagementException;  
  8. import java.security.NoSuchAlgorithmException;  
  9. import java.security.cert.CertificateException;  
  10. import java.security.cert.X509Certificate;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13. import java.util.Map;  
  14.   
  15. import javax.net.ssl.SSLContext;  
  16. import javax.net.ssl.SSLException;  
  17. import javax.net.ssl.SSLSession;  
  18. import javax.net.ssl.SSLSocket;  
  19. import javax.net.ssl.TrustManager;  
  20. import javax.net.ssl.X509TrustManager;  
  21.   
  22. import org.apache.commons.logging.Log;  
  23. import org.apache.commons.logging.LogFactory;  
  24. import org.apache.http.Consts;  
  25. import org.apache.http.HttpEntity;  
  26. import org.apache.http.HttpResponse;  
  27. import org.apache.http.HttpStatus;  
  28. import org.apache.http.NameValuePair;  
  29. import org.apache.http.ParseException;  
  30. import org.apache.http.client.ClientProtocolException;  
  31. import org.apache.http.client.HttpClient;  
  32. import org.apache.http.client.config.RequestConfig;  
  33. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  34. import org.apache.http.client.methods.CloseableHttpResponse;  
  35. import org.apache.http.client.methods.HttpGet;  
  36. import org.apache.http.client.methods.HttpPost;  
  37. import org.apache.http.client.utils.URLEncodedUtils;  
  38. import org.apache.http.conn.scheme.Scheme;  
  39. import org.apache.http.conn.ssl.SSLSocketFactory;  
  40. import org.apache.http.conn.ssl.X509HostnameVerifier;  
  41. import org.apache.http.entity.StringEntity;  
  42. import org.apache.http.impl.client.CloseableHttpClient;  
  43. import org.apache.http.impl.client.DefaultHttpClient;  
  44. import org.apache.http.impl.client.HttpClients;  
  45. import org.apache.http.message.BasicNameValuePair;  
  46. import org.apache.http.protocol.HTTP;  
  47. import org.apache.http.util.EntityUtils;  
  48.    
  49. /** 
  50.  * 封装了一些采用HttpClient发送HTTP请求的方法 
  51.  * @see 本工具所采用的是最新的HttpComponents-Client-4.2.1 
  52.  */  
  53. public class HttpClientUtil {  
  54.     private HttpClientUtil(){}  
  55.        
  56.     private static Log logger = LogFactory.getLog(HttpClientUtil.class);  
  57.       
  58.       
  59.     /** 
  60.      * 发送HTTP_GET请求 
  61.      * @see 该方法会自动关闭连接,释放资源 
  62.      * @param requestURL    请求地址(含参数) 
  63.      * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 
  64.      * @return 远程主机响应正文 
  65.      */  
  66.     public static String sendGetRequest(String reqURL, String decodeCharset){  
  67.         long responseLength = 0;       //响应长度  
  68.         String responseContent = null//响应内容  
  69.         HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例  
  70.         HttpGet httpGet = new HttpGet(reqURL);           //创建org.apache.http.client.methods.HttpGet  
  71.         try{  
  72.             HttpResponse response = httpClient.execute(httpGet); //执行GET请求  
  73.             HttpEntity entity = response.getEntity();            //获取响应实体  
  74.             if(null != entity){  
  75.                 responseLength = entity.getContentLength();  
  76.                 responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset);  
  77.                 EntityUtils.consume(entity); //Consume response content  
  78.             }  
  79.             System.out.println("请求地址: " + httpGet.getURI());  
  80.             System.out.println("响应状态: " + response.getStatusLine());  
  81.             System.out.println("响应长度: " + responseLength);  
  82.             System.out.println("响应内容: " + responseContent);  
  83.         }catch(ClientProtocolException e){  
  84.             logger.debug("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e);  
  85.         }catch(ParseException e){  
  86.             logger.debug(e.getMessage(), e);  
  87.         }catch(IOException e){  
  88.             logger.debug("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e);  
  89.         }finally{  
  90.             httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源  
  91.         }  
  92.         return responseContent;  
  93.     }  
  94.        
  95.        
  96.     /** 
  97.      * 发送HTTP_POST请求 
  98.      * @see 该方法为<code>sendPostRequest(String,String,boolean,String,String)</code>的简化方法 
  99.      * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 
  100.      * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code> 
  101.      * @param isEncoder 用于指明请求数据是否需要UTF-8编码,true为需要 
  102.      */  
  103.     public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder){  
  104.         return sendPostRequest(reqURL, sendData, isEncoder, nullnull);  
  105.     }  
  106.        
  107.        
  108.     /** 
  109.      * 发送HTTP_POST请求 
  110.      * @see 该方法会自动关闭连接,释放资源 
  111.      * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> 
  112.      * @param reqURL        请求地址 
  113.      * @param sendData      请求参数,若有多个参数则应拼接成param11=value11&22=value22&33=value33的形式后,传入该参数中 
  114.      * @param isEncoder     请求数据是否需要encodeCharset编码,true为需要 
  115.      * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 
  116.      * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 
  117.      * @return 远程主机响应正文 
  118.      */  
  119.     public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder, String encodeCharset, String decodeCharset){  
  120.         String responseContent = null;  
  121.         HttpClient httpClient = new DefaultHttpClient();  
  122.            
  123.         HttpPost httpPost = new HttpPost(reqURL);  
  124.         //httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8");  
  125.         httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");  
  126.         try{  
  127.             if(isEncoder){  
  128.                 List formParams = new ArrayList();  
  129.                 for(String str : sendData.split("&")){  
  130.                     formParams.add(new BasicNameValuePair(str.substring(0,str.indexOf("=")), str.substring(str.indexOf("=")+1)));  
  131.                 }  
  132.                 httpPost.setEntity(new StringEntity(URLEncodedUtils.format(formParams, encodeCharset==null ? "UTF-8" : encodeCharset)));  
  133.             }else{  
  134.                 httpPost.setEntity(new StringEntity(sendData));  
  135.             }  
  136.                
  137.             HttpResponse response = httpClient.execute(httpPost);  
  138.             HttpEntity entity = response.getEntity();  
  139.             if (null != entity) {  
  140.                 responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset);  
  141.                 EntityUtils.consume(entity);  
  142.             }  
  143.         }catch(Exception e){  
  144.             logger.debug("与[" + reqURL + "]通信过程中发生异常,堆栈信息如下", e);  
  145.         }finally{  
  146.             httpClient.getConnectionManager().shutdown();  
  147.         }  
  148.         return responseContent;  
  149.     }  
  150.        
  151.        
  152.     /** 
  153.      * 发送HTTP_POST请求 
  154.      * @see 该方法会自动关闭连接,释放资源 
  155.      * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> 
  156.      * @param reqURL        请求地址 
  157.      * @param params        请求参数 
  158.      * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 
  159.      * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 
  160.      * @return 远程主机响应正文 
  161.      */  
  162.     public static String sendPostRequest(String reqURL, Map params, String encodeCharset, String decodeCharset){  
  163.         String responseContent = null;  
  164.         HttpClient httpClient = new DefaultHttpClient();  
  165.            
  166.         HttpPost httpPost = new HttpPost(reqURL);  
  167.         List formParams = new ArrayList(); //创建参数队列  
  168.         for(Map.Entry entry : params.entrySet()){  
  169.             formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  170.         }  
  171.         try{  
  172.             httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset==null ? "UTF-8" : encodeCharset));  
  173.                
  174.             HttpResponse response = httpClient.execute(httpPost);  
  175.             HttpEntity entity = response.getEntity();  
  176.             if (null != entity) {  
  177.                 responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset);  
  178.                 EntityUtils.consume(entity);  
  179.             }  
  180.         }catch(Exception e){  
  181.             logger.debug("与[" + reqURL + "]通信过程中发生异常,堆栈信息如下", e);  
  182.         }finally{  
  183.             httpClient.getConnectionManager().shutdown();  
  184.         }  
  185.         return responseContent;  
  186.     }  
  187.        
  188.        
  189.     /** 
  190.      * 发送HTTPS_POST请求 
  191.      * @see 该方法为<code>sendPostSSLRequest(String,Map,String,String)</code>方法的简化方法 
  192.      * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 
  193.      * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code> 
  194.      */  
  195.     public static String sendPostSSLRequest(String reqURL, Map params){  
  196.         return sendPostSSLRequest(reqURL, params, nullnull);  
  197.     }  
  198.        
  199.        
  200.     /** 
  201.      * 发送HTTPS_POST请求 
  202.      * @see 该方法会自动关闭连接,释放资源 
  203.      * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> 
  204.      * @param reqURL        请求地址 
  205.      * @param params        请求参数 
  206.      * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 
  207.      * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 
  208.      * @return 远程主机响应正文 
  209.      */  
  210.     public static String sendPostSSLRequest(String reqURL, Map params, String encodeCharset, String decodeCharset){  
  211.         String responseContent = "";  
  212.         HttpClient httpClient = new DefaultHttpClient();  
  213.         X509TrustManager xtm = new X509TrustManager(){  
  214.             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  215.             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  216.             public X509Certificate[] getAcceptedIssuers() {return null;}  
  217.         };  
  218.         try {  
  219.             SSLContext ctx = SSLContext.getInstance("TLS");  
  220.             ctx.init(nullnew TrustManager[]{xtm}, null);  
  221.             SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);  
  222.             httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https"443, socketFactory));  
  223.                
  224.             HttpPost httpPost = new HttpPost(reqURL);  
  225.             List formParams = new ArrayList();  
  226.             for(Map.Entry entry : params.entrySet()){  
  227.                 formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  228.             }  
  229.             httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset==null ? "UTF-8" : encodeCharset));  
  230.                
  231.             HttpResponse response = httpClient.execute(httpPost);  
  232.             HttpEntity entity = response.getEntity();  
  233.             if (null != entity) {  
  234.                 responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset);  
  235.                 EntityUtils.consume(entity);  
  236.             }  
  237.         } catch (Exception e) {  
  238.             logger.debug("与[" + reqURL + "]通信过程中发生异常,堆栈信息为", e);  
  239.         } finally {  
  240.             httpClient.getConnectionManager().shutdown();  
  241.         }  
  242.         return responseContent;  
  243.     }  
  244.        
  245.        
  246.     /** 
  247.      * 发送HTTP_POST请求 
  248.      * @see 若发送的<code>params</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code> 
  249.      * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒 
  250.      * @param reqURL 请求地址 
  251.      * @param params 发送到远程主机的正文数据,其数据类型为<code>java.util.Map</code> 
  252.      * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code> 
  253.      */  
  254.     public static String sendPostRequestByJava(String reqURL, Map params){  
  255.         StringBuilder sendData = new StringBuilder();  
  256.         for(Map.Entry entry : params.entrySet()){  
  257.             sendData.append(entry.getKey()).append("=").append(entry.getValue()).append("&");  
  258.         }  
  259.         if(sendData.length() > 0){  
  260.             sendData.setLength(sendData.length() - 1); //删除最后一个&符号  
  261.         }  
  262.         return sendPostRequestByJava(reqURL, sendData.toString());  
  263.     }  
  264.        
  265.        
  266.     /** 
  267.      * 发送HTTP_POST请求 
  268.      * @see 若发送的<code>sendData</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code> 
  269.      * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒 
  270.      * @param reqURL   请求地址 
  271.      * @param sendData 发送到远程主机的正文数据 
  272.      * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code> 
  273.      */  
  274.     public static String sendPostRequestByJava(String reqURL, String sendData){  
  275.         HttpURLConnection httpURLConnection = null;  
  276.         OutputStream out = null//写  
  277.         InputStream in = null;   //读  
  278.         int httpStatusCode = 0;  //远程主机响应的HTTP状态码  
  279.         try{  
  280.             URL sendUrl = new URL(reqURL);  
  281.             httpURLConnection = (HttpURLConnection)sendUrl.openConnection();  
  282.             httpURLConnection.setRequestMethod("POST");  
  283.             httpURLConnection.setDoOutput(true);        //指示应用程序要将数据写入URL连接,其值默认为false  
  284.             httpURLConnection.setUseCaches(false);  
  285.             httpURLConnection.setConnectTimeout(30000); //30秒连接超时  
  286.             httpURLConnection.setReadTimeout(30000);    //30秒读取超时  
  287.                
  288.             out = httpURLConnection.getOutputStream();  
  289.             out.write(sendData.toString().getBytes());  
  290.                
  291.             //清空缓冲区,发送数据  
  292.             out.flush();  
  293.                
  294.             //获取HTTP状态码  
  295.             httpStatusCode = httpURLConnection.getResponseCode();  
  296.                
  297.             in = httpURLConnection.getInputStream();              
  298.             byte[] byteDatas = new byte[in.available()];  
  299.             in.read(byteDatas);  
  300.             return new String(byteDatas) + "`" + httpStatusCode;  
  301.         }catch(Exception e){  
  302.             logger.debug(e.getMessage());  
  303.             return "Failed`" + httpStatusCode;  
  304.         }finally{  
  305.             if(out != null){  
  306.                 try{  
  307.                     out.close();  
  308.                 }catch (Exception e){  
  309.                     logger.debug("关闭输出流时发生异常,堆栈信息如下", e);  
  310.                 }  
  311.             }  
  312.             if(in != null){  
  313.                 try{  
  314.                     in.close();  
  315.                 }catch(Exception e){  
  316.                     logger.debug("关闭输入流时发生异常,堆栈信息如下", e);  
  317.                 }  
  318.             }  
  319.             if(httpURLConnection != null){  
  320.                 httpURLConnection.disconnect();  
  321.                 httpURLConnection = null;  
  322.             }  
  323.         }  
  324.     }  
  325.       
  326.     /** 
  327.      * https posp请求,可以绕过证书校验 
  328.      * @param url 
  329.      * @param params 
  330.      * @return 
  331.      */  
  332.     public static final String sendHttpsRequestByPost(String url, Map params) {  
  333.         String responseContent = null;  
  334.         HttpClient httpClient = new DefaultHttpClient();  
  335.         //创建TrustManager  
  336.         X509TrustManager xtm = new X509TrustManager() {  
  337.             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  338.             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  339.             public X509Certificate[] getAcceptedIssuers() {  
  340.                 return null;  
  341.             }  
  342.         };  
  343.         //这个好像是HOST验证  
  344.         X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {  
  345.             public boolean verify(String arg0, SSLSession arg1) {  
  346.                 return true;  
  347.             }  
  348.             public void verify(String arg0, SSLSocket arg1) throws IOException {}  
  349.             public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {}  
  350.             public void verify(String arg0, X509Certificate arg1) throws SSLException {}  
  351.         };  
  352.         try {  
  353.             //TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext  
  354.             SSLContext ctx = SSLContext.getInstance("TLS");  
  355.             //使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用  
  356.             ctx.init(nullnew TrustManager[] { xtm }, null);  
  357.             //创建SSLSocketFactory  
  358.             SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);  
  359.             socketFactory.setHostnameVerifier(hostnameVerifier);  
  360.             //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上  
  361.             httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443));  
  362.             HttpPost httpPost = new HttpPost(url);  
  363.             List formParams = new ArrayList(); // 构建POST请求的表单参数  
  364.             for (Map.Entry entry : params.entrySet()) {  
  365.                 formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  366.             }  
  367.             httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));  
  368.             HttpResponse response = httpClient.execute(httpPost);  
  369.             HttpEntity entity = response.getEntity(); // 获取响应实体  
  370.             if (entity != null) {  
  371.                 responseContent = EntityUtils.toString(entity, "UTF-8");  
  372.             }  
  373.         } catch (KeyManagementException e) {  
  374.             e.printStackTrace();  
  375.         } catch (NoSuchAlgorithmException e) {  
  376.             e.printStackTrace();  
  377.         } catch (UnsupportedEncodingException e) {  
  378.             e.printStackTrace();  
  379.         } catch (ClientProtocolException e) {  
  380.             e.printStackTrace();  
  381.         } catch (ParseException e) {  
  382.             e.printStackTrace();  
  383.         } catch (IOException e) {  
  384.             e.printStackTrace();  
  385.         } finally {  
  386.             // 关闭连接,释放资源  
  387.             httpClient.getConnectionManager().shutdown();  
  388.         }  
  389.         return responseContent;  
  390.     }  
  391.       
  392.       
  393.     /** 
  394.      * 发送HTTP_POST请求,json格式数据 
  395.      * @param url 
  396.      * @param body 
  397.      * @return 
  398.      * @throws Exception 
  399.      */  
  400.     public static String sendPostByJson(String url, String body) throws Exception {  
  401.         CloseableHttpClient httpclient = HttpClients.custom().build();  
  402.         HttpPost post = null;  
  403.         String resData = null;  
  404.         CloseableHttpResponse result = null;  
  405.         try {  
  406.             post = new HttpPost(url);  
  407.             HttpEntity entity2 = new StringEntity(body, Consts.UTF_8);  
  408.             post.setConfig(RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build());  
  409.             post.setHeader("Content-Type""application/json");  
  410.             post.setEntity(entity2);  
  411.             result = httpclient.execute(post);  
  412.             if (HttpStatus.SC_OK == result.getStatusLine().getStatusCode()) {  
  413.                 resData = EntityUtils.toString(result.getEntity());  
  414.             }  
  415.         } finally {  
  416.             if (result != null) {  
  417.                 result.close();  
  418.             }  
  419.             if (post != null) {  
  420.                 post.releaseConnection();  
  421.             }  
  422.             httpclient.close();  
  423.         }  
  424.         return resData;  
  425.     }  
  426.        
  427.       
  428. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值