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 小时前
两台电脑直连网络不通怎么办?布莱克60515 小时前
HTTP 状态码详解:从分类到实战StevenSurpass17 小时前
打印业务彻底解耦:FastPrintAgent 统一 JSON 模型 + FastReport 引擎的设计与落地执明wa18 小时前
Android RecyclerView 多类型, 多种 Itemcaptain37620 小时前
网络原理(7)-NAT(Network Address Translation)wl851120 小时前
SAP CPI 教程004 如何通过groovy修改XML节点属性值爱研究的小梁20 小时前
异构链路科普:5G/4G、卫星、专网、WiFi、光纤,如何聚合工作?梦想平凡21 小时前
情怀棋牌源代码焕新记录(一):全新国风UI与原版工程梳理K成长日志21 小时前
DALI-2协议简介Shota Kishi1 天前
Solana Shreds 的 UDP 直投架构:Shredstream UDP Forwarding 的设计与实现