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
相关推荐
科技块儿11 分钟前
反羊毛实战复盘:IP查询+设备指纹联动风控方案掘根1 小时前
【仿Muduo库项目】HTTP模块3——HttpContext子模块瓦尔登湖懒羊羊1 小时前
到底能不能把HTTP1.0讲明白了小李独爱秋2 小时前
计算机网络经典问题透视:TLS协议工作过程全景解析不穿格子的程序员3 小时前
计算机网络篇1:OSI + HTTP进化史 + 三次握手四次挥手常温5104 小时前
关于网卡如何给CPU减负方法及策略莫小墨4 小时前
Modbus协议汤愈韬4 小时前
防火墙双机热备技术之VRRP北京耐用通信4 小时前
告别通信干扰与距离限制:耐达讯自动化Profibus总线光纤中继器赋能伺服驱动器稳定连接乾元5 小时前
10 个可复制的企业级项目:从需求到交付的 AI 网络工程模板(深度实战版)