|  | @ -0,0 +1,231 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.util.http;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import org.apache.http.HttpEntity;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.ParseException;
 | 
	
		
			
				|  |  | import org.apache.http.client.ClientProtocolException;
 | 
	
		
			
				|  |  | import org.apache.http.client.entity.UrlEncodedFormEntity;
 | 
	
		
			
				|  |  | import org.apache.http.client.methods.CloseableHttpResponse;
 | 
	
		
			
				|  |  | import org.apache.http.client.methods.HttpGet;
 | 
	
		
			
				|  |  | import org.apache.http.client.methods.HttpPost;
 | 
	
		
			
				|  |  | import org.apache.http.impl.client.CloseableHttpClient;
 | 
	
		
			
				|  |  | import org.apache.http.impl.client.HttpClients;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.util.EntityUtils;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.*;
 | 
	
		
			
				|  |  | import java.net.HttpURLConnection;
 | 
	
		
			
				|  |  | import java.net.URL;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.Arrays;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | public class HttpClientUtil {
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送post请求
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url     请求地址
 | 
	
		
			
				|  |  |      * @param params  请求参数
 | 
	
		
			
				|  |  |      * @param chatSet 编码格式
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public  String post(String url, List<NameValuePair> params, String chatSet) {
 | 
	
		
			
				|  |  |         // 创建默认的httpClient实例.
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         // 创建httppost
 | 
	
		
			
				|  |  |         HttpPost httppost = new HttpPost(url);
 | 
	
		
			
				|  |  |         UrlEncodedFormEntity uefEntity;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             uefEntity = new UrlEncodedFormEntity(params, chatSet);
 | 
	
		
			
				|  |  |             httppost.setEntity(uefEntity);
 | 
	
		
			
				|  |  |             CloseableHttpResponse response = httpclient.execute(httppost);
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 HttpEntity entity = response.getEntity();
 | 
	
		
			
				|  |  |                 if (entity != null) {
 | 
	
		
			
				|  |  |                     return EntityUtils.toString(entity, chatSet);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             } finally {
 | 
	
		
			
				|  |  |                 response.close();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } catch (UnsupportedEncodingException e1) {
 | 
	
		
			
				|  |  |             e1.printStackTrace();
 | 
	
		
			
				|  |  |         } catch (IOException e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             // 关闭连接,释放资源
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 httpclient.close();
 | 
	
		
			
				|  |  |             } catch (IOException e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 发送get请求
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url     请求地址
 | 
	
		
			
				|  |  |      * @param chatSet 编码格式
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public  String get(String url, String chatSet) {
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             // 创建httpget.
 | 
	
		
			
				|  |  |             HttpGet httpget = new HttpGet(url);
 | 
	
		
			
				|  |  |             // 执行get请求.
 | 
	
		
			
				|  |  |             CloseableHttpResponse response = httpclient.execute(httpget);
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 // 获取响应实体
 | 
	
		
			
				|  |  |                 HttpEntity entity = response.getEntity();
 | 
	
		
			
				|  |  |                 if (entity != null) {
 | 
	
		
			
				|  |  |                     return EntityUtils.toString(entity, chatSet);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             } finally {
 | 
	
		
			
				|  |  |                 response.close();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } catch (ParseException e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } catch (IOException e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             // 关闭连接,释放资源
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 httpclient.close();
 | 
	
		
			
				|  |  |             } catch (IOException e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * http调用方法,(健康之路开放平台)
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public  String httpPost(String url, Map<String, String> params) throws Exception {
 | 
	
		
			
				|  |  |         CloseableHttpClient httpclient = HttpClients.createDefault();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             HttpPost httpPost = new HttpPost(url);
 | 
	
		
			
				|  |  |             if (params != null && params.size() > 0) {
 | 
	
		
			
				|  |  |                 List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
 | 
	
		
			
				|  |  |                 for (Map.Entry<String, String> entry : params.entrySet()) {
 | 
	
		
			
				|  |  |                     NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
 | 
	
		
			
				|  |  |                     valuePairs.add(nameValuePair);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(valuePairs, "UTF-8");
 | 
	
		
			
				|  |  |                 httpPost.setEntity(formEntity);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             CloseableHttpResponse resp = httpclient.execute(httpPost);
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 HttpEntity entity = resp.getEntity();
 | 
	
		
			
				|  |  |                 String respContent = EntityUtils.toString(entity, "UTF-8").trim();
 | 
	
		
			
				|  |  |                 return respContent;
 | 
	
		
			
				|  |  |             } finally {
 | 
	
		
			
				|  |  |                 resp.close();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             httpclient.close();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取加密后参数集合(健康之路开放平台)
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public  Map<String, String> getSecretParams(Map<String, String> params, String appId, String secret) {
 | 
	
		
			
				|  |  |         String timestamp = Long.toString(System.currentTimeMillis());
 | 
	
		
			
				|  |  |         params.put("timestamp", timestamp);
 | 
	
		
			
				|  |  |         StringBuilder stringBuilder = new StringBuilder();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 对参数名进行字典排序  
 | 
	
		
			
				|  |  |         String[] keyArray = params.keySet().toArray(new String[0]);
 | 
	
		
			
				|  |  |         Arrays.sort(keyArray);
 | 
	
		
			
				|  |  |         // 拼接有序的参数名-值串  
 | 
	
		
			
				|  |  |         stringBuilder.append(appId);
 | 
	
		
			
				|  |  |         for (String key : keyArray) {
 | 
	
		
			
				|  |  |             stringBuilder.append(key).append(params.get(key));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String codes = stringBuilder.append(secret).toString();
 | 
	
		
			
				|  |  |         String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
 | 
	
		
			
				|  |  |         // 添加签名,并发送请求  
 | 
	
		
			
				|  |  |         params.put("appId", appId);
 | 
	
		
			
				|  |  |         params.put("sign", sign);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return params;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 向指定 URL 发送POST方法的请求
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      *            发送请求的 URL带上参数
 | 
	
		
			
				|  |  |      * @param param
 | 
	
		
			
				|  |  |      *            POST参数。
 | 
	
		
			
				|  |  |      * @return 所代表远程资源的响应结果
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public  String sendPost(String url, String param) {
 | 
	
		
			
				|  |  |         StringBuffer buffer = new StringBuffer();
 | 
	
		
			
				|  |  |         PrintWriter out = null;
 | 
	
		
			
				|  |  |         BufferedReader in = null;
 | 
	
		
			
				|  |  |         HttpURLConnection conn = null;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             URL realUrl = new URL(url);
 | 
	
		
			
				|  |  |             // 打开和URL之间的连接
 | 
	
		
			
				|  |  |             conn = (HttpURLConnection) realUrl.openConnection();
 | 
	
		
			
				|  |  |             conn.setRequestMethod("POST");
 | 
	
		
			
				|  |  |             conn.setDoOutput(true);
 | 
	
		
			
				|  |  |             conn.setDoInput(true);
 | 
	
		
			
				|  |  |             conn.setUseCaches(false);
 | 
	
		
			
				|  |  |             conn.setRequestProperty("Content-Type", "application/json");
 | 
	
		
			
				|  |  |             conn.setRequestProperty("accept", "*/*");
 | 
	
		
			
				|  |  |             conn.setRequestProperty("connection", "Keep-Alive");
 | 
	
		
			
				|  |  |             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 | 
	
		
			
				|  |  |             OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
 | 
	
		
			
				|  |  |             osw.write(param.toString());
 | 
	
		
			
				|  |  |             osw.flush();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // 读取返回内容
 | 
	
		
			
				|  |  |             BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
 | 
	
		
			
				|  |  |             String temp;
 | 
	
		
			
				|  |  |             while ((temp = br.readLine()) != null) {
 | 
	
		
			
				|  |  |                 buffer.append(temp);
 | 
	
		
			
				|  |  |                 buffer.append("\n");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |            e.printStackTrace();
 | 
	
		
			
				|  |  |         } finally {
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 if (out != null) {
 | 
	
		
			
				|  |  |                     out.close();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (in != null) {
 | 
	
		
			
				|  |  |                     in.close();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             } catch (IOException ex) {
 | 
	
		
			
				|  |  |                 ex.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return buffer.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |