¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.NameValuePair; |
| | | 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.web.client.RestTemplate; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.PrintStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Restæå¡å¸®å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class RestHelper { |
| | | private static RestTemplate restTemplate; |
| | | |
| | | private final static Log log = LogFactory.getLog(RestHelper.class); |
| | | |
| | | /** |
| | | * è·åRestTemplate |
| | | * |
| | | * @return RestTemplate |
| | | */ |
| | | public static RestTemplate getRestTemplate() { |
| | | if (restTemplate == null) { |
| | | restTemplate = SpringContextHelper.getBean(RestTemplate.class); |
| | | } |
| | | |
| | | return restTemplate; |
| | | } |
| | | |
| | | /** |
| | | * Get请æ±-HttpURLConnection |
| | | * |
| | | * @param url URLå°å |
| | | * @return å符串 |
| | | * @throws IOException IOå¼å¸¸ |
| | | */ |
| | | public static String getForConn(String url) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | try { |
| | | URL restUrl = new URL(url); |
| | | |
| | | conn = (HttpURLConnection) restUrl.openConnection(); |
| | | // POST,GET,PUT,DELETE |
| | | conn.setRequestMethod("GET"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | |
| | | br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| | | |
| | | String line; |
| | | StringBuilder sb = new StringBuilder(); |
| | | while ((line = br.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } finally { |
| | | if (br != null) { |
| | | br.close(); |
| | | } |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请æ±-HttpURLConnection |
| | | * |
| | | * @param url URLå°å |
| | | * @param query æ¥è¯¢æ¡ä»¶ |
| | | * @return å符串 |
| | | * @throws IOException IOå¼å¸¸ |
| | | */ |
| | | public static String postForConn(String url, String query) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | try { |
| | | URL restUrl = new URL(url); |
| | | |
| | | conn = (HttpURLConnection) restUrl.openConnection(); |
| | | // POST,GET,PUT,DELETE |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setDoOutput(true); |
| | | |
| | | PrintStream ps = new PrintStream(conn.getOutputStream()); |
| | | ps.print(query); |
| | | ps.close(); |
| | | |
| | | // OutputStream out = conn.getOutputStream() |
| | | // out.write(query.getBytes()) |
| | | // out.close() |
| | | |
| | | br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| | | |
| | | String line; |
| | | StringBuilder sb = new StringBuilder(); |
| | | while ((line = br.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } finally { |
| | | if (br != null) { |
| | | br.close(); |
| | | } |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Get请æ±-CloseableHttpClient |
| | | * |
| | | * @param uri Uriå°å |
| | | * @return ååºå符串 |
| | | */ |
| | | public static String get(String uri) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | | |
| | | HttpGet httpGet = new HttpGet(uri); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpGet); |
| | | // ååºè¿åä½ |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请æ±-CloseableHttpClient |
| | | * |
| | | * @param uri Uriå°å |
| | | * @param postData å¾
åéæ°æ® |
| | | * @return ååºå符串 |
| | | */ |
| | | public static String post(String uri, List<NameValuePair> postData) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | | |
| | | UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postData, StaticData.TEXT_ENCODER); |
| | | HttpPost httpPost = new HttpPost(uri); |
| | | httpPost.setEntity(postEntity); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpPost); |
| | | |
| | | // ååºè¿åä½ |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Get请æ±-RestTemplate |
| | | * |
| | | * @param uri Uriå°å |
| | | * @return ååºå符串 |
| | | */ |
| | | public static String getForRest(String uri) { |
| | | try { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(uri, String.class); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请æ±-RestTemplate |
| | | * |
| | | * @param uri Uriå°å |
| | | * @param postData å¾
åéæ°æ® |
| | | * @return ååºå符串 |
| | | */ |
| | | public static String postForRest(String uri, List<NameValuePair> postData) { |
| | | try { |
| | | RestTemplate rest = getRestTemplate(); |
| | | UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData, StaticData.TEXT_ENCODER); |
| | | |
| | | return rest.postForObject(uri, entity, String.class); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | public static String postForRest(String uri, Map<String, Object> map) { |
| | | try { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, map, String.class); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åéè¯¯ä¿¡æ¯ |
| | | * |
| | | * @param uri Uriå°å |
| | | * @param ex å¼å¸¸ |
| | | * @return éè¯¯ä¿¡æ¯ |
| | | */ |
| | | public static String getErrorInfo(String uri, Exception ex) { |
| | | Map<String, Object> map = new LinkedHashMap<>(); |
| | | map.put("result", null); |
| | | map.put("message", ex.getMessage()); |
| | | map.put("code", 400); |
| | | map.put("uri", uri); |
| | | //map.put("tag", StaticData.CACHE_PREFIX) |
| | | |
| | | return map.toString(); |
| | | } |
| | | } |