|  | @ -0,0 +1,138 @@
 | 
												
													
														
															|  | 
 |  | package com.yihu.jw.care.util;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.alibaba.fastjson.JSONObject;
 | 
												
													
														
															|  | 
 |  | 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.util.EntityUtils;
 | 
												
													
														
															|  | 
 |  | import org.springframework.http.HttpHeaders;
 | 
												
													
														
															|  | 
 |  | import org.springframework.http.MediaType;
 | 
												
													
														
															|  | 
 |  | import org.springframework.web.client.RestTemplate;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import java.io.IOException;
 | 
												
													
														
															|  | 
 |  | import java.io.UnsupportedEncodingException;
 | 
												
													
														
															|  | 
 |  | import java.util.List;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | public class HttpClientUtil {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 	/**
 | 
												
													
														
															|  | 
 |  | 	 * 发送post请求
 | 
												
													
														
															|  | 
 |  | 	 * @param url 请求地址
 | 
												
													
														
															|  | 
 |  | 	 * @param params 请求参数
 | 
												
													
														
															|  | 
 |  | 	 * @param chatSet 编码格式
 | 
												
													
														
															|  | 
 |  | 	 * @return
 | 
												
													
														
															|  | 
 |  | 	 */
 | 
												
													
														
															|  | 
 |  | 	public static 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 static 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;
 | 
												
													
														
															|  | 
 |  | 	}
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 	public static String postBodyStr(String url, String params){
 | 
												
													
														
															|  | 
 |  | 		RestTemplate restTemplate = new RestTemplate();
 | 
												
													
														
															|  | 
 |  | 		HttpHeaders headers = new HttpHeaders();
 | 
												
													
														
															|  | 
 |  | 		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
 | 
												
													
														
															|  | 
 |  | 		headers.setContentType(type);
 | 
												
													
														
															|  | 
 |  | 		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
 | 
												
													
														
															|  | 
 |  | 		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params, headers);
 | 
												
													
														
															|  | 
 |  | 		String ret = restTemplate.postForObject(url, formEntity, String.class);
 | 
												
													
														
															|  | 
 |  | 		return ret;
 | 
												
													
														
															|  | 
 |  | 	}
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 	public static String postBody(String url, JSONObject params){
 | 
												
													
														
															|  | 
 |  | 		RestTemplate restTemplate = new RestTemplate();
 | 
												
													
														
															|  | 
 |  | 		HttpHeaders headers = new HttpHeaders();
 | 
												
													
														
															|  | 
 |  | 		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
 | 
												
													
														
															|  | 
 |  | 		headers.setContentType(type);
 | 
												
													
														
															|  | 
 |  | 		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
 | 
												
													
														
															|  | 
 |  | 		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
 | 
												
													
														
															|  | 
 |  | 		String ret = restTemplate.postForObject(url, formEntity, String.class);
 | 
												
													
														
															|  | 
 |  | 		return ret;
 | 
												
													
														
															|  | 
 |  | 	}
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 	public static void putBody(String url,JSONObject params){
 | 
												
													
														
															|  | 
 |  | 		RestTemplate restTemplate = new RestTemplate();
 | 
												
													
														
															|  | 
 |  | 		HttpHeaders headers = new HttpHeaders();
 | 
												
													
														
															|  | 
 |  | 		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
 | 
												
													
														
															|  | 
 |  | 		headers.setContentType(type);
 | 
												
													
														
															|  | 
 |  | 		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
 | 
												
													
														
															|  | 
 |  | 		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
 | 
												
													
														
															|  | 
 |  | 		restTemplate.put(url, formEntity, String.class);
 | 
												
													
														
															|  | 
 |  | 	}
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | }
 |