标签:set cat 异常 超时 ram print result stack ring
1 private static int SOCKET_TIME_OUT = 60*1000; //传输间隔超时 2 private static int CONNECT_TIME_OUT = 60*1000; //链接建立超时 3 4 /** 5 * @author Yanzm 6 * @param url 请求路径 7 * @param jsonString Json格式的请求参数 8 * @return 9 */ 10 public static String doPost(String url,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("请求响应正常,返回的数据为空"); 33 return resultStr; 34 } 35 }else { 36 log.error("响应异常,状态码为:{}",response.getStatusLine().getStatusCode()); 37 return StringUtils.EMPTY; 38 } 39 } catch (Exception e) { 40 log.error("httpClient 接口调用异常"); 41 e.printStackTrace(); 42 }finally { 43 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 关闭异常"); 56 e.printStackTrace(); 57 } 58 } 59 } 60 return resultStr; 61 } 62 63 /** 64 * @author Yanzm 65 * @param url 请求路径 66 * @return 67 */ 68 public static String 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) { 85 resultStr = EntityUtils.toString(response.getEntity(), "UTF-8"); 86 if (StringUtils.isNotBlank(resultStr)) { 87 return resultStr; 88 }else { 89 log.info("请求响应正常,返回的数据为空"); 90 return resultStr; 91 } 92 }else { 93 log.error("响应异常,状态码为:{}",response.getStatusLine().getStatusCode()); 94 return StringUtils.EMPTY; 95 } 96 } catch (Exception e) { 97 log.error("httpClient 接口调用异常"); 98 e.printStackTrace(); 99 }finally { 100 if (response != null) { 101 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 }
标签:set cat 异常 超时 ram print result stack ring
原文地址:https://www.cnblogs.com/yan-zm/p/12629452.html