| | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * POST JSON 字符串,Content-Type: application/json; charset=utf-8 |
| | | */ |
| | | public static String doPostJsonBody(String url, String jsonBody) { |
| | | CloseableHttpClient httpClient = null; |
| | | CloseableHttpResponse httpResponse = null; |
| | | String result = ""; |
| | | httpClient = HttpClients.createDefault(); |
| | | HttpPost httpPost = new HttpPost(url); |
| | | RequestConfig requestConfig = RequestConfig.custom() |
| | | .setConnectTimeout(35000) |
| | | .setConnectionRequestTimeout(35000) |
| | | .setSocketTimeout(60000) |
| | | .build(); |
| | | httpPost.setConfig(requestConfig); |
| | | httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); |
| | | try { |
| | | httpPost.setEntity(new StringEntity(jsonBody, "utf-8")); |
| | | httpResponse = httpClient.execute(httpPost); |
| | | HttpEntity entity = httpResponse.getEntity(); |
| | | result = EntityUtils.toString(entity); |
| | | } catch (ClientProtocolException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (null != httpResponse) { |
| | | try { |
| | | httpResponse.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (null != httpClient) { |
| | | try { |
| | | httpClient.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |