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
相关推荐
captain3766 小时前
网络原理(3)-TCP核心机制连接管理▲▲▲易岳群6 小时前
搞清 Socket、TCP、MQTT 的通信关系:从底层协议到物联网实战宵时待雨7 小时前
linux笔记归纳19:网络层协议IP只睡四小时7 小时前
内网穿透+WebSocket:Node 零依赖远程桌面实战Jeremy_WW7 小时前
802.3协议解读 05:116章节 200 Gb/s 和 400 Gb/s 网络介绍 V梦想平凡8 小时前
百游棋牌源代码开发搭建教程(七):WebSocket房间同步与断线恢复Lhappy嘻嘻9 小时前
网络(六)|应用层 HTTP&HTTPS:URL、请求响应、Cookie 与证书筝筝ba10 小时前
如何跳过FFDCtachibana211 小时前
WebSocket 和 SSE 通信的区别及局限性为思念酝酿的痛12 小时前
Linux网络进程守护