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
相关推荐
执明wa12 小时前
LayoutInflater详解: XML是如何变成View的?牛马工作号18 小时前
Wi‑Fi 完全指南:从 802.11 协议到 Wi‑Fi 7、AC+AP 与 Mesh 工程组网Geek-Chow18 小时前
ALB SSL policy conflict (AWS Load Balancer Controller)爱刷碗的苏泓舒18 小时前
FTP、FTPS 与 WinSCP:原理、连接机制及 GNSS 工程应用雲帝19 小时前
Windows虚拟机UDP大包分片排障ControlRookie19 小时前
第04篇_把 HTTP 放进 PLC:扫描周期、缓冲区、状态机和能力边界八代臻20 小时前
蒲公英免费异地组网記億揺晃着的那天20 小时前
HTTPS 页面内网直连 NAS:解决 Mixed Content 与公网带宽瓶颈想学好C++的oMen1 天前
socket编程TCPGitLqr2 天前
别再盲目复制了:彻底搞懂 CORS 的本质与那些“神坑”