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
相关推荐
xixingzhe26 小时前
AI 对话打字机效果:SSE 和 HTTP 的区别与适用场景叼烟扛炮12 小时前
Linux 学习笔记(五十一):数据链路层与 ARP 协议——局域网寻址与黑客骗局沫璃染墨18 小时前
从零入门计算机网络系列(十四):TCP报文与字节流本质——从数据封装到报文边界byte轻骑兵20 小时前
【LMP】规范精讲[13]: 参数的户口本、公钥的集装箱与出厂的兜底值流浪00120 小时前
计算机网络篇17:传输层——TCP 超时重传、三次握手与四次挥手,TIME_WAIT 为什么要等 2MSLAI 编程助手GPT2 天前
curl 返回成功就能收文件?用本地 HTTP 验证错误页与超时流浪0012 天前
计算机网络篇16:传输层——TCP 报头属性字段:16 位窗口大小撑起流量控制,六个标志位标识报文类型apollo_qwe2 天前
接口超时了,前端为什么不能直接重试?Jongkhurun2 天前
以太网的最小帧长为什么是 64 字节:从 CSMA/CD 冲突窗口推导 64 Bytehonsor2 天前
SNMP 读取以太网温湿度传感器数据,监控平台对接实操