HttpClient Toolbar

  . 1     Private  static  int SOCKET_TIME_OUT = 60 * 1000;         // transmission timeout interval       
  2      Private  static  int CONNECT_TIME_OUT = 60 * 1000;         // link establishment timeout 
  . 3      
  . 4      / ** 
  . 5       * @author Yanzm
   . 6       * @param URL request path
   7       * @ param request parameters jsonString Json format
   . 8       * @return 
  . 9       * / 
10      public  static String the doPost (URL String, String JSONString) {
 . 11         CloseableHttpClient httpClient= HttpClients.createDefault();
 12         CloseableHttpResponse response = null;
 13         String resultStr = null;
 14         HttpPost post = new HttpPost(url);
 15         
 16         RequestConfig requestConfig = RequestConfig.custom()
 17                 .setSocketTimeout(SOCKET_TIME_OUT)
 18                 .setConnectTimeout(CONNECT_TIME_OUT).build();
 19         post.setConfig(requestConfig);
 20         
 21         try {
 22             post.addHeader("Accept","application/json");
 23             StringEntity entity = new StringEntity(jsonString);
 24         
 25             post.setEntity(entity);
 26             response = httpClient.execute(post);
 27             if (response.getStatusLine().getStatusCode() == 200) {
 28                 resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
 29                 if (StringUtils.isNotBlank(resultStr)) {
 30                     return resultStr;
 31                 }else {
 32                     log.info ( "normal response to the request, the returned data is empty" );
 33 is                      return ResultStr;
 34 is                  }
 35              } the else {
 36                  log.error ( "abnormal response status code is: {}" , response.getStatusLine (). getStatusCode ());
 37 [                  return StringUtils.EMPTY;
 38 is              }
 39          } the catch (exception E) {
 40              log.error ( "interface call httpClient exception" );
 41 is              e.printStackTrace ();
 42 is          } the finally {
 43 is              IF (response != null) {
 44                 try {
 45                     response.close();
 46                 } catch (IOException e) {
 47                     log.error("httpClient response 关闭异常");
 48                     e.printStackTrace();
 49                 }
 50             }
 51             if (httpClient != null) {
 52                 try {
 53                     httpClient.close();
 54                 } catch (IOException e) {
55                      log.error ( "httpClient closing abnormality" );
 56 is                      e.printStackTrace ();
 57 is                  }
 58              }
 59          }
 60          return ResultStr;
 61 is      }
 62 is      
63 is      / ** 
64       * @author Yanzm
 65       * @param   URL request path
 66       * @return 
67       * / 
68      public  static String the doGet (String URL) {
 69          CloseableHttpClient httpClient = HttpClients.createDefault();
 70         CloseableHttpResponse response = null;
 71         String resultStr = null;
 72         HttpGet get = new HttpGet(url);
 73         
 74         RequestConfig requestConfig = RequestConfig.custom()
 75                 .setSocketTimeout(SOCKET_TIME_OUT)
 76                 .setConnectTimeout(CONNECT_TIME_OUT).build();
 77         get.setConfig(requestConfig);
 78         
 79         try {
 80             get.addHeader("Accept","application/json");
81              
82              Response = httpClient.execute (GET);
 83              log.info ( "Connect to {}" , URL);
 84              IF (. Response.getStatusLine () getStatusCode () == 200 is ) {
 85                  ResultStr EntityUtils.toString = ( response.getEntity (), "UTF-. 8" );
 86                  IF (StringUtils.isNotBlank (ResultStr)) {
 87                      return ResultStr;
 88                  } the else {
 89                      log.info ( "normal response to the request, the returned data is empty" );
 90                      return ResultStr;
 91 is                  }
92              } the else {
 93                  log.error ( "abnormal response status code is: {}" ., Response.getStatusLine () getStatusCode ());
 94                  return StringUtils.EMPTY;
 95              }
 96          } the catch (Exception E) {
 97              log .error ( "httpClient abnormal call Interface" );
 98              e.printStackTrace ();
 99          } the finally {
 100              IF (! Response = null ) {
 101                  the try {
 102                      response.close ();
 103                 } catch (IOException e) {
104                     log.error("httpClient response 关闭异常");
105                     e.printStackTrace();
106                 }
107             }
108             if (httpClient != null) {
109                 try {
110                     httpClient.close();
111                 } catch (IOException e) {
112                     log.error("httpClient 关闭异常");
113                     e.printStackTrace();
114                 }
115             }
116         }
117         return resultStr;
118     }
119     
120     public static void setSocketTimeOut(int timeOut) {
121         SOCKET_TIME_OUT = timeOut;
122     }
123     
124     public static void setConnectTimeOut(int timeOut) {
125         CONNECT_TIME_OUT = timeOut;
126     }

 

Guess you like

Origin www.cnblogs.com/yan-zm/p/12629452.html