public static void httpPost(String url, String xml) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/xml"); connection.setRequestProperty("Content-Length", String.valueOf(xml.length())); connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(xml.getBytes()); outputStream.flush(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { response.append(line); } bufferedReader.close(); System.out.println("请求成功: " + response.toString()); } else { System.out.println("请求失败,响应头:" + connection.getHeaderFields()); System.out.println("请求失败,响应码:" + connection.getResponseCode()); } }
Http请求Body中携带raw XML形式
于 2024-08-27 11:31:00 首次发布