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
相关推荐
wangl_9221 小时前
Modbus RTU 与 Modbus TCP 深入指南-附录:快速参考表wangl_921 天前
Modbus RTU 与 Modbus TCP 深入指南-决策树与选型建议小船跨境1 天前
ChatGPT助力高效网页数据抓取实战JiaWen技术圈1 天前
DTLS 基础m0_738120721 天前
ctfshow靶场SSRF部分——基础绕过到协议攻击解题思路与技巧(二)Ogcloud_oversea1 天前
SD-WAN 技术架构解析:控制平面与数据平面的解耦实践树下水月1 天前
HTTPS 站点请求 HTTP的API 接口服务报错的问题XiYang-DING1 天前
【Java EE】 HTTP协议wangl_921 天前
Modbus RTU 与 Modbus TCP 深入指南-结束语皮卡祺q1 天前
【Java网络编程核心-0】从OSI七层到TCP/IP模型深度解析(HTTP协议、TCP、UDP 、 IP协议)