| | |
| | | import org.springframework.web.util.UriComponentsBuilder; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.PrintWriter; |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URI; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | |
| | | return result; |
| | | } |
| | | |
| | | public static String sendPostToken(String url, String json, String authorization) { |
| | | String result = ""; |
| | | try { |
| | | URL realUrl = new URL(url); |
| | | HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | | connection.setRequestProperty("Content-Type", "application/json"); |
| | | connection.setRequestProperty("Authorization", authorization); |
| | | connection.setDoOutput(true); |
| | | |
| | | // 发送POST请求的数据 |
| | | try (OutputStream os = connection.getOutputStream()) { |
| | | os.write(json.getBytes()); |
| | | os.flush(); |
| | | } |
| | | // 获取响应码和响应体 |
| | | int responseCode = connection.getResponseCode(); |
| | | if (responseCode == HttpURLConnection.HTTP_OK) { // 200 OK |
| | | try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| | | StringBuilder response = new StringBuilder(); |
| | | String responseLine; |
| | | while ((responseLine = br.readLine()) != null) { |
| | | response.append(responseLine.trim()); |
| | | } |
| | | result = response.toString(); |
| | | } |
| | | } else { |
| | | System.out.println("POST request not worked"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |