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
相关推荐
令狐掌门2 小时前
tcp长连接与短连接创小匠2 小时前
创客匠人洞察:AI 时代创始人 IP 打造如何突破效率与价值的平衡__只是为了好玩__2 小时前
Apache http 强制 httpszhysunny3 小时前
WebSocket实战:实现实时聊天应用 - 双向通信技术详解hrrrrb3 小时前
【TCP/IP】5. IP 协议hhh123987_3 小时前
以太网基础⑤UDP 协议原理与 FPGA 实现灰海3 小时前
封装WebSocketDemonAvenger3 小时前
实现可靠的 UDP 协议:Go 语言版本YC运维4 小时前
OSPF实验以及核心原理全解老六ip加速器4 小时前
ip地址可以精确到什么级别?如何获取/更改ip地址