| | |
| | | } |
| | | |
| | | |
| | | public static String sendPost(String httpUrl, Map<String, Object> maps) { |
| | | public static String sendPost(String httpUrl, Map<String, Object> maps,Map<String ,Object> headers) { |
| | | |
| | | try { |
| | | // 1. 创建 URL 对象 |
| | |
| | | connection.setRequestMethod("POST"); |
| | | // 4. 设置 Content-Type 头部字段 |
| | | connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
| | | if (headers != null && !headers.isEmpty()){ |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()) { |
| | | connection.setRequestProperty(entry.getKey(), entry.getValue().toString()); |
| | | } |
| | | } |
| | | // 6. 向服务器发送数据 |
| | | String requestBody = JSONUtil.toJsonStr(maps); |
| | | log.info(httpUrl+"入参: "+requestBody); |
| | | log.info("请求头 ->{}",JSONUtil.toJsonStr(headers)); |
| | | // String requestBody1 = maps.toString(); |
| | | byte[] postData = requestBody.getBytes("UTF-8"); |
| | | byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8); |
| | | connection.setDoOutput(true); |
| | | try (OutputStream outputStream = connection.getOutputStream()) { |
| | | outputStream.write(postData); |