Source:
public void handleException(final String jobName, final Throwable cause) { if (dingtalkConfiguration == null) { dingtalkConfiguration = DingtalkEnvironment.getINSTANCE().getDingtalkConfiguration(); } HttpURLConnection connection = null; try { URL url = getUrl(); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setConnectTimeout(dingtalkConfiguration.getConnectTimeout()); connection.setReadTimeout(dingtalkConfiguration.getReadTimeout()); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.connect(); OutputStream outputStream = connection.getOutputStream(); String msg = getMsg(jobName, cause); String paramJson = getParamJson(msg); outputStream.write(paramJson.getBytes(StandardCharsets.UTF_8)); int code = connection.getResponseCode(); if (HttpURLConnection.HTTP_OK == code) { InputStream resultInputStream = connection.getInputStream(); StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } JsonObject resp = GsonFactory.getGson().fromJson(result.toString(), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("Job '{}' exception occur in job processing, But the notification Dingtalk failure, error is : {}", jobName, resp.get("errmsg").getAsString(), cause); } } } catch (IOException | NoSuchAlgorithmException | InvalidKeyException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } }

Target:
public void handleException(final String jobName, final Throwable cause) { if (null == dingtalkConfiguration) { dingtalkConfiguration = DingtalkEnvironment.getInstance().getDingtalkConfiguration(); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(dingtalkConfiguration.getConnectTimeout()) .setSocketTimeout(dingtalkConfiguration.getReadTimeout()).build(); httpPost.setConfig(requestConfig); String paramJson = getParamJson(getMsg(jobName, cause)); StringEntity entity = new StringEntity(paramJson, StandardCharsets.UTF_8.name()); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); CloseableHttpResponse response = httpclient.execute(httpPost); try { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by Dingtalk because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to Dingtalk was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by Dingtalk because of: Unexpected response status: {}", jobName, status, cause); } } finally { response.close(); } }