| | |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.OutputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | HttpEntity entity = response.getEntity(); |
| | | return EntityUtils.toString(entity, "UTF-8"); |
| | | } |
| | | case 603: { |
| | | HttpEntity entity = response.getEntity(); |
| | | return EntityUtils.toString(entity, "UTF-8"); |
| | | } |
| | | case 302: { |
| | | return sendGetHttp(response.getFirstHeader("location").getValue(), ""); |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | public static String sendPostRequest(String targetUrl, Map<String, Object> requestData) throws IOException { |
| | | // 创建连接 |
| | | URL url = new URL(targetUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | |
| | | // 设置请求方法为POST |
| | | connection.setRequestMethod("POST"); |
| | | |
| | | // 启用输入输出流 |
| | | connection.setDoOutput(true); |
| | | connection.setDoInput(true); |
| | | |
| | | // 设置请求头 |
| | | connection.setRequestProperty("Content-Type", "application/json"); |
| | | |
| | | // 将Map转换为JSON字符串 |
| | | String jsonInputString = convertMapToJson(requestData); |
| | | |
| | | // 写入请求体 |
| | | try (OutputStream os = connection.getOutputStream()) { |
| | | byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 获取响应代码 |
| | | int responseCode = connection.getResponseCode(); |
| | | System.out.println("Response Code: " + responseCode); |
| | | |
| | | // 读取响应 |
| | | StringBuilder response = new StringBuilder(); |
| | | try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| | | String inputLine; |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | } |
| | | |
| | | // 返回响应内容 |
| | | return response.toString(); |
| | | } |
| | | |
| | | |
| | | public static String convertMapToJson(Map<String, Object> map) { |
| | | StringBuilder jsonString = new StringBuilder("{"); |
| | | |
| | | for (Map.Entry<String, Object> entry : map.entrySet()) { |
| | | if (jsonString.length() > 1) { |
| | | jsonString.append(","); |
| | | } |
| | | jsonString.append("\"").append(entry.getKey()).append("\":"); |
| | | |
| | | if (entry.getValue() instanceof Map) { |
| | | jsonString.append(convertMapToJson((Map<String, Object>) entry.getValue())); |
| | | } else if (entry.getValue() instanceof List) { |
| | | jsonString.append("["); |
| | | List<?> list = (List<?>) entry.getValue(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | if (i > 0) { |
| | | jsonString.append(","); |
| | | } |
| | | jsonString.append(convertMapToJson((Map<String, Object>) list.get(i))); |
| | | } |
| | | jsonString.append("]"); |
| | | } else { |
| | | jsonString.append("\"").append(entry.getValue()).append("\""); |
| | | } |
| | | } |
| | | jsonString.append("}"); |
| | | |
| | | return jsonString.toString(); |
| | | } |
| | | |
| | | } |