支持各类型报文与参数说明
说明:
-
- public static String postJsonBody(String url, int timeout, String contentType, String requestBody, String encoding){
- HttpPost post = new HttpPost(url);
- try {
- post.setHeader("Content-type", contentType);
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(timeout)
- .setConnectTimeout(timeout)
- .setConnectionRequestTimeout(timeout)
- .setExpectContinueEnabled(false).build();
- post.setConfig(requestConfig);
-
- post.setEntity(new StringEntity(requestBody, encoding));
- logger.info("[HttpUtils Post] begin invoke url:" + url + " , params:" + requestBody);
- CloseableHttpResponse response = httpclient.execute(post);
- try {
- HttpEntity entity = response.getEntity();
- try {
- if(entity != null){
- String str = EntityUtils.toString(entity, encoding);
- logger.info("[HttpUtils Post]Debug response, url :" + url + " , response string :"+str);
- return str;
- }
- } finally {
- if(entity != null){
- entity.getContent().close();
- }
- }
- } finally {
- if(response != null){
- response.close();
- }
- }
- } catch (UnsupportedEncodingException e) {
- logger.error("UnsupportedEncodingException", e);
- } catch (Exception e) {
- logger.error("Exception", e);
- } finally {
- post.releaseConnection();
- }
- return "";
- }