| | |
| | | return null; |
| | | } |
| | | |
| | | public static String sendPostToken(String httpUrl, Map<String, Object> maps,String authorization) { |
| | | |
| | | 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"); |
| | | connection.setRequestProperty("Authorization", authorization); |
| | | // 6. 向服务器发送数据 |
| | | String requestBody = JSONUtil.toJsonStr(maps); |
| | | log.info(httpUrl+"入参: "+requestBody); |
| | | // String requestBody1 = maps.toString(); |
| | | byte[] postData = requestBody.getBytes("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; |
| | | } |
| | | |
| | | /** |
| | | * 发送 post请求 |
| | | * |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | public static String sendPost(String httpUrl, Map<String, Object> maps,String token) { |
| | | |
| | | 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"); |
| | | //设置请求token |
| | | connection.setRequestProperty("Authorization",token); |
| | | // 6. 向服务器发送数据 |
| | | String requestBody = JSONUtil.toJsonStr(maps); |
| | | log.info(httpUrl+"入参: "+requestBody); |
| | | // String requestBody1 = maps.toString(); |
| | | byte[] postData = requestBody.getBytes("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; |
| | | } |
| | | |
| | | |
| | | } |