1. 程式人生 > >rest接口webservice接口利用http請求方式發送數據

rest接口webservice接口利用http請求方式發送數據

name tst adl build ring new resp sock ota

  1 package com.sinoservices.bms.bbl.common.util;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.nio.charset.Charset;
  7 
  8 import org.apache.http.client.config.RequestConfig;
  9 import org.apache.http.client.methods.CloseableHttpResponse;
10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.entity.StringEntity; 13 import org.apache.http.impl.client.CloseableHttpClient; 14 import org.apache.http.impl.client.HttpClients; 15 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
16 import org.slf4j.Logger; 17 import org.slf4j.LoggerFactory; 18 19 import com.alibaba.fastjson.JSONObject; 20 import com.alibaba.fastjson.serializer.SerializerFeature; 21 22 23 /** 24 * 25 * @Description http調用工具類 26 * @author Lynch.Feng 27 * @Date 2018年10月26日 下午3:53:03
28 * @version 1.0.0 29 */ 30 public class HttpUtil { 31 private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class); 32 33 private HttpUtil() { 34 35 } 36 37 private static CloseableHttpClient httpClient; 38 39 static { 40 PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); 41 cm.setMaxTotal(100); 42 cm.setDefaultMaxPerRoute(20); 43 cm.setDefaultMaxPerRoute(50); 44 httpClient = HttpClients.custom().setConnectionManager(cm).build(); 45 } 46 47 public static String get(String url) { 48 CloseableHttpResponse response = null; 49 BufferedReader in = null; 50 String result = ""; 51 try { 52 HttpGet httpGet = new HttpGet(url); 53 RequestConfig requestConfig = 54 RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build(); 55 httpGet.setConfig(requestConfig); 56 httpGet.setConfig(requestConfig); 57 httpGet.addHeader("Content-type", "application/json; charset=utf-8"); 58 httpGet.setHeader("Accept", "application/json"); 59 response = httpClient.execute(httpGet); 60 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 61 StringBuilder sb = new StringBuilder(); 62 String line = ""; 63 while ((line = in.readLine()) != null) { 64 sb.append(line); 65 } 66 in.close(); 67 result = sb.toString(); 68 } catch (IOException e) { 69 logger.error("http request error", e); 70 } finally { 71 try { 72 if (null != response) { 73 response.close(); 74 } 75 } catch (IOException e) { 76 logger.error("close response error", e); 77 } 78 } 79 return result; 80 } 81 82 public static String postJson(String url, String jsonString) { 83 CloseableHttpResponse response = null; 84 BufferedReader in = null; 85 String result = ""; 86 try { 87 HttpPost httpPost = new HttpPost(url); 88 RequestConfig requestConfig = 89 RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build(); 90 httpPost.setConfig(requestConfig); 91 httpPost.setConfig(requestConfig); 92 httpPost.addHeader("Content-type", "application/json; charset=utf-8"); 93 httpPost.setHeader("Accept", "application/json"); 94 httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8"))); 95 response = httpClient.execute(httpPost); 96 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 97 StringBuilder sb = new StringBuilder(); 98 String line = ""; 99 while ((line = in.readLine()) != null) { 100 sb.append(line); 101 } 102 in.close(); 103 result = sb.toString(); 104 } catch (IOException e) { 105 logger.error("http request error", e); 106 } finally { 107 try { 108 if (null != response) { 109 response.close(); 110 } 111 } catch (IOException e) { 112 logger.error("close response error", e); 113 } 114 } 115 return result; 116 } 117 }

rest接口webservice接口利用http請求方式發送數據