// 发送GET请求
public static String sendGet(String orderCode, String requestUrl) throws Exception {
logger.info("requestUrl-start:" + requestUrl);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;ide
try { URIBuilder uriBuilder = new URIBuilder(requestUrl); uriBuilder.addParameter("orderNo", orderCode); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8.toString()); response = httpClient.execute(httpGet); int errorCode = response.getStatusLine().getStatusCode(); logger.info("sendOrderCenterPost-status:" + errorCode); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, "utf-8"); } } catch (Exception e) { logger.error("sendOrderCenterPost-exception" + orderCode, e); } finally { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } return null; }
// 发送POST请求
public static String resendPost(String reqJsonData, String requestUrl) throws Exception {
logger.info("requestUrl-start:" + requestUrl);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;ui
try { HttpPost httpPost = new HttpPost(requestUrl); httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8.toString()); httpPost.setEntity(new StringEntity(reqJsonData, "UTF-8")); response = httpClient.execute(httpPost); int errorCode = response.getStatusLine().getStatusCode(); logger.info("发送请求的响应状态:" + errorCode); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, "utf-8"); } } catch (Exception e) { logger.error("重推日志系统异常", e); } finally { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } return null; } 说明: 通常经常使用的响应格式是定义一个JSON对象,因此,对GET/POST请求返回的字符串能够用 JSON.parseObject(resStr, ReturnObject.class); 进行解析