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
相关推荐
爱和冰阔落1 小时前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战流浪0011 天前
计算机网络篇25:应用层——HTTPS 协议原理:非对称加密协商密钥,CA 证书挡住中间人FlightYe1 天前
音视频修炼之容器与协议(六):HTTP 协议小飞在努力1 天前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化wdfk_prog1 天前
LWIP教程 33:从 `https_ex_init()` 到 `http_recv()`——altcp、mbedTLS、TLS Handshake 与 HTTPS 数据通路buhuizhiyuci2 天前
websocket的介绍沫璃染墨2 天前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组大今野3 天前
第 16 章计算机网络体系结构详解:OSI七层模型、TCP/IP四层协议、三次握手与四次挥手LabVIEW开发3 天前
Ethernet/IP 会话无效 -251723770nice_lcj5203 天前
【从零理解计算机网络 · 15】完整串联:一次请求的全旅程,以及那道经典面试题