Barra de herramientas de HttpClient

  . 1     privada  estática  int SOCKET_TIME_OUT = 60 * 1000;         // tiempo de espera de transmisión de intervalo de       
  2      privada  estática  int CONNECT_TIME_OUT = 60 * 1000;         // establecimiento del enlace de tiempo de espera 
  . 3      
  . 4      / ** 
  . 5       * @author Yanzm
   . 6       * @param URL de petición de ruta
   7       * @ PARAM parámetros de la petición jsonString JSON formato
   . 8       * @return 
  . 9       * / 
10      pública  estática cadena doPost (URL String, String jsonString) {
 . 11         CloseableHttpClient HttpClient = HttpClients.createDefault ();
12          respuesta CloseableHttpResponse = nulo ;
13          Cadena resultStr = nulo ;
14          HttpPost post = nuevo 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 ( "Aceptar", "application / json" );
23              entidad StringEntity = nuevo StringEntity (jsonString);
24          
25              post.setEntity (entidad);
26              de respuesta = httpClient.execute (post);
27              Si (response.getStatusLine () getStatusCode () == 200. ) {
 28                  resultStr = EntityUtils.toString (response.getEntity (), "UTF-8" );
29                  si (StringUtils.isNotBlank (resultStr)) {
 30                      de retorno resultStr;
31                  } demás {
 32                     log.info ( "respuesta normal a la solicitud, los datos devueltos está vacía" );
 33 es                      el retorno ResultStr;
 34 es                  }
 35              } else {
 36                  log.error ( "código de estado de respuesta anormal es: {}" , response.getStatusLine (). getStatusCode ());
 37 [                  volver StringUtils.EMPTY;
 38 es              }
 39          } la captura (excepción E) {
 40              log.error ( "llamada interfaz HttpClient excepción" );
 41 es              e.printStackTrace ();
 42 se          } el fin {
 43 es              SI(! = respuesta nula ) {
 44                  try {
 45                      response.close ();
46                  } catch (IOException e) {
 47                      log.error ( "respuesta HttpClient关闭异常" );
48                      e.printStackTrace ();
49                  }
 50              }
 51              si (HttpClient =! Nula ) {
 52                  try {
 53                      httpClient.close ();
54                  } catch (IOException e) {
55                      log.error ( "HttpClient anormalidad de cierre" );
 56 es                      e.printStackTrace ();
 57 es                  }
 58              }
 59          }
 60          de retorno ResultStr;
 61 es      }
 62 es      
63 es      / ** 
64       * @author Yanzm
 65       * @param   URL de petición de ruta
 66       * @return 
67       * / 
68      pública  estática cadena doGet (String url) {
 69          CloseableHttpClient HttpClient =HttpClients.createDefault ();
70          respuesta CloseableHttpResponse = nulo ;
71          Cadena resultStr = nulo ;
72          HttpGet get = nueva 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 ( "Aceptar", "application / json" );
81              
82              Respuesta = httpClient.execute (GET);
 83              log.info ( "Conectar a {}" , URL);
 84              SI (. Response.getStatusLine () getStatusCode () == 200 es ) {
 85                  ResultStr EntityUtils.toString = ( response.getEntity (), "UTF-8". );
 86                  IF (StringUtils.isNotBlank (ResultStr)) {
 87                      retorno ResultStr;
 88                  } else {
 89                      log.info ( "respuesta normal a la solicitud, los datos devueltos está vacía" );
 90                      de retorno ResultStr;
 91 es                  }
92              } else {
 93                  log.error ( "código de estado de respuesta anormal es: {}" , Response.getStatusLine () getStatusCode ().);
 94                  de retorno StringUtils.EMPTY;
 95              }
 96          } la captura (Exception e) {
 97              log .Error ( "HttpClient llamada anormal Interface" );
 98              e.printStackTrace ();
 99          } finally {
 100              SI (Respuesta =! nula ) {
 101                  el try {
 102                      response.close ();
 103                 } Catch (IOException e) {
 104                      log.error ( "HttpClient respuesta关闭异常" );
105                      e.printStackTrace ();
106                  }
 107              }
 108              Si (! HttpClient = nulo ) {
 109                  try {
 110                      httpClient.close ();
111                  } catch (IOException e) {
 112                      log.error ( "HttpClient关闭异常" );
113                      e.printStackTrace ();
114                 }
 115              }
 116          }
 117          retorno resultStr;
118      }
 119      
120      público  estático  void setSocketTimeOut ( int timeout) {
 121          SOCKET_TIME_OUT = timeOut ha;
122      }
 123      
124      pública  estática  vacío setConnectTimeOut ( int timeout) {
 125          CONNECT_TIME_OUT = timeOut ha;
126      }

 

Supongo que te gusta

Origin www.cnblogs.com/yan-zm/p/12629452.html
Recomendado
Clasificación