From c4ea4cf098ec9fb74aa82530970c9c2e98aec6b8 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期二, 15 四月 2025 14:46:50 +0800 Subject: [PATCH] 同步字典从数据库查询 --- src/main/java/com/example/utils/HttpClientUtils.java | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/utils/HttpClientUtils.java b/src/main/java/com/example/utils/HttpClientUtils.java index 1856869..6cb36c2 100644 --- a/src/main/java/com/example/utils/HttpClientUtils.java +++ b/src/main/java/com/example/utils/HttpClientUtils.java @@ -97,7 +97,54 @@ } - 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 瀵硅薄 @@ -109,10 +156,10 @@ // 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); @@ -186,6 +233,7 @@ BufferedReader reader = null; StringBuilder response = new StringBuilder(); try { + log.info("璇锋眰鍦板潃 ->{}",httpUrl); URL url = new URL(httpUrl); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); @@ -195,6 +243,7 @@ } 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("&"); @@ -203,6 +252,7 @@ .append("=") .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8")); } + log.info("鍙傛暟鎷兼帴 ->{}",postData); writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(postData.toString()); writer.flush(); @@ -211,6 +261,7 @@ while ((line = reader.readLine()) != null) { response.append(line); } + log.info("杩斿洖 ->{}",response); } catch (Exception e) { e.printStackTrace(); return null; @@ -236,6 +287,7 @@ 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"); @@ -244,6 +296,7 @@ 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); @@ -259,6 +312,7 @@ while ((line = reader.readLine()) != null) { response.append(line); } + log.info("杩斿洖 ->{}",response); } catch (Exception e) { e.printStackTrace(); return null; -- Gitblit v1.8.0