| | |
| | | package com.sandu.ximon.admin.utils; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.sandu.ximon.admin.utils.request.CommonHeader; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.client.utils.URIBuilder; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicHeader; |
| | |
| | | private static final String UTF8 = "utf-8"; |
| | | |
| | | |
| | | public static String get(String url){ |
| | | public static String get(String url) { |
| | | CloseableHttpClient client = HttpClients.createDefault(); |
| | | HttpGet get = new HttpGet(url); |
| | | String result = null; |
| | |
| | | HttpResponse httpResponse = client.execute(get); |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | |
| | | result = streamToString(inStream,UTF8); |
| | | result = streamToString(inStream, UTF8); |
| | | |
| | | inStream.close(); |
| | | } catch (Exception e) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | public static String GetWithHeader (String url, List<NameValuePair> urlParameters, Map<String, String> header) throws URISyntaxException { |
| | | public static String GetWithHeader(String url, List<NameValuePair> urlParameters, Map<String, String> header) throws URISyntaxException { |
| | | |
| | | |
| | | URI finalURI = new URIBuilder(url).setParameters(urlParameters).build(); |
| | | CloseableHttpClient client = HttpClients.createDefault(); |
| | | HttpGet get = new HttpGet(finalURI); |
| | | for(String key: header.keySet()) { |
| | | get.setHeader(key, header.get(key)); |
| | | } |
| | | String result = null; |
| | | try { |
| | | URI finalURI = new URIBuilder(url).setParameters(urlParameters).build(); |
| | | CloseableHttpClient client = HttpClients.createDefault(); |
| | | HttpGet get = new HttpGet(finalURI); |
| | | for (String key : header.keySet()) { |
| | | get.setHeader(key, header.get(key)); |
| | | } |
| | | String result = null; |
| | | try { |
| | | // StringEntity s = new StringEntity(params, UTF8); |
| | | // s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, |
| | | // "application/json")); |
| | | HttpResponse httpResponse = client.execute(get); |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | HttpResponse httpResponse = client.execute(get); |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | |
| | | result = streamToString(inStream, UTF8); |
| | | result = streamToString(inStream, UTF8); |
| | | |
| | | inStream.close(); |
| | | } catch (Exception e) { |
| | | return null; |
| | | inStream.close(); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return result; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | public static String PostWithHeader (String URL, String json, CommonHeader header) { |
| | | public static String PostWithHeader(String URL, String json, CommonHeader header) { |
| | | System.out.println("****************************************"); |
| | | CloseableHttpClient client = HttpClients.createDefault(); |
| | | HttpPost post = new HttpPost(URL); |
| | | for(String key: header.keySet()) { |
| | | for (String key : header.keySet()) { |
| | | post.setHeader(key, header.get(key)); |
| | | } |
| | | String result = null; |
| | |
| | | post.setEntity(s); |
| | | HttpResponse httpResponse = client.execute(post); |
| | | InputStream inStream = httpResponse.getEntity().getContent(); |
| | | System.out.println("post: " + JSON.toJSONString(post)); |
| | | |
| | | result = streamToString(inStream,UTF8); |
| | | result = streamToString(inStream, UTF8); |
| | | |
| | | inStream.close(); |
| | | } catch (Exception e) { |
| | | System.out.println("error" + e.getMessage()); |
| | | System.out.println("error" + e.getMessage()); |
| | | return null; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | public static String streamToString(InputStream in, String encoding){ |
| | | public static String streamToString(InputStream in, String encoding) { |
| | | // 将流转换为字符串 |
| | | try { |
| | | StringBuffer sb = new StringBuffer(); |
| | | byte[] b = new byte[1024]; |
| | | for (int n; (n = in.read(b)) != -1;) { |
| | | for (int n; (n = in.read(b)) != -1; ) { |
| | | sb.append(new String(b, 0, n, encoding)); |
| | | } |
| | | return sb.toString(); |
| | | } catch (IOException e) { |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("提取 requestBody 异常", e); |
| | | } |