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
相关推荐
国服第二切图仔5 小时前
Electron for 鸿蒙PC实战案例Gitcode口袋工具之HTTP请求封装的技术实现与设计解析s09071366 小时前
ZYNQ DMA to UDP 数据传输系统设计文档hazy1k8 小时前
ESP32基础-Socket通信 (TCP/UDP)xinxinhenmeihao9 小时前
爬虫为什么要用动态ip?动态IP在爬虫中起到哪些作用?特种加菲猫11 小时前
用户数据报协议(UDP)详解cccyi711 小时前
HTTP 协议详解:从基础到核心特性渡我白衣12 小时前
五种IO模型与非阻塞IO重启的码农13 小时前
enet源码解析(7): 跨平台套接字调用抽象层特种加菲猫13 小时前
HTTP协议核心机制解析重生之我是Java开发战士15 小时前
【Java SE】HTTP与HTTPS通信协议