| | |
| | | } |
| | | |
| | | |
| | | 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 对象 |
| | | URL url = new URL(httpUrl); |
| | | // 2. 创建 HttpURLConnection 对象 |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | // 3. 设置请求方法为 POST |
| | | 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(StandardCharsets.UTF_8); |
| | | connection.setDoOutput(true); |
| | | try (OutputStream outputStream = connection.getOutputStream()) { |
| | | outputStream.write(postData); |
| | | } |
| | | |
| | | // 8. 获取响应数据 |
| | | try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { |
| | | String line; |
| | | StringBuilder response = new StringBuilder(); |
| | | while ((line = reader.readLine()) != null) { |
| | | response.append(line); |
| | | } |
| | | log.info("====================================================="); |
| | | log.info(httpUrl+"出参"); |
| | | log.info(response.toString()); |
| | | connection.disconnect(); |
| | | return response.toString(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | public static String sendPost(String httpUrl, String json) { |
| | | |
| | | try { |
| | | // 1. 创建 URL 对象 |
| | |
| | | // 4. 设置 Content-Type 头部字段 |
| | | connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
| | | // 6. 向服务器发送数据 |
| | | String requestBody = JSONUtil.toJsonStr(maps); |
| | | String requestBody = json; |
| | | log.info(httpUrl+"入参: "+requestBody); |
| | | // 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); |
| | |
| | | BufferedReader reader = null; |
| | | StringBuilder response = new StringBuilder(); |
| | | try { |
| | | log.info("请求地址 ->{}",httpUrl); |
| | | URL url = new URL(httpUrl); |
| | | connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | |
| | | } |
| | | connection.setDoOutput(true); |
| | | StringBuilder postData = new StringBuilder(); |
| | | log.info("入参 ->{}", JSONUtil.toJsonStr(maps)); |
| | | for (Map.Entry<String, Object> entry : maps.entrySet()) { |
| | | if (postData.length() > 0) { |
| | | postData.append("&"); |
| | |
| | | .append("=") |
| | | .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8")); |
| | | } |
| | | log.info("参数拼接 ->{}",postData); |
| | | writer = new OutputStreamWriter(connection.getOutputStream()); |
| | | writer.write(postData.toString()); |
| | | writer.flush(); |
| | |
| | | while ((line = reader.readLine()) != null) { |
| | | response.append(line); |
| | | } |
| | | log.info("返回 ->{}",response); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | |
| | | String boundary = "----WebKitFormBoundary" + UUID.randomUUID().toString().replaceAll("-", ""); |
| | | String CRLF = "\r\n"; |
| | | try { |
| | | log.info("请求地址 ->{}",httpUrl); |
| | | URL url = new URL(httpUrl); |
| | | connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | |
| | | connection.setRequestProperty("Authorization", authorization); |
| | | } |
| | | connection.setDoOutput(true); |
| | | log.info("入参 ->{}",maps); |
| | | outStream = new DataOutputStream(connection.getOutputStream()); |
| | | for (Map.Entry<String, Object> entry : maps.entrySet()) { |
| | | outStream.writeBytes("--" + boundary + CRLF); |
| | |
| | | while ((line = reader.readLine()) != null) { |
| | | response.append(line); |
| | | } |
| | | log.info("返回 ->{}",response); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |