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形式
demodashi6662024-08-29 19:18
相关推荐
略懂而已21 小时前
安装SSL后网站速度变慢?Thanks_ks1 天前
HTTPS:浏览器地址栏那把锁的工作原理杨凯凡1 天前
Spring Boot 大文件下载如何实现断点续传?HTTP Range、206 与 416 实战吴建旭 智宅焕2 天前
智能家居全国交付中执行端能力跃迁的架构设计:从标准装调到交付专家的系统化路径Dream Cosmos2 天前
传输层协议之UDP梦到啥说啥2 天前
Linux网络编程:UDP网络协议FungLeo2 天前
成为全栈·Vue 管理后台篇·用 fetch 建一层可控的请求内核张小姐的猫2 天前
【基于 WebSocket 的多用户网页五子棋】—— 项目结构设计与实用工具类模块wuyk5552 天前
Spider-第03讲-HTTP速通与读懂响应梦到啥说啥3 天前
Linux网络编程:TCP网络协议