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
相关推荐
牛油果子哥q8 小时前
C++对接LLM完整工程:异步HTTP请求、JSON解析、超时容错、重连兜底呆呆敲代码的小Y9 小时前
TCP / UDP 对比介绍SKH.9 小时前
网络(4)HTTP协议与TCP并发服务器你不是我我12 小时前
【AI 测评】群晖 NAS 部署 Vaultwarden:从 HTTPS 到浏览器自动填充的完整密码管理流程IT大白鼠19 小时前
Ingress 与 Ingress-Controller:K8s 七层 HTTP 反向代理技术详解与实践明月_清风21 小时前
MCP vs ACP vs LSP:AI 时代三大协议的「三足鼎立」我爱cope1 天前
【计算机网络 | 传输层3:TCP 协议概述:面向连接、可靠传输到底意味着什么?】myy-learn1 天前
30-HTTP协议开开心心就好1 天前
免费刷题软件推荐,支持导入题库自动判题幼儿园蛋小黄1 天前
基于 TCP 的嵌入式网络通信学习总结