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
相关推荐
吴建旭 智宅焕7 小时前
智能家居全国交付中执行端能力跃迁的架构设计:从标准装调到交付专家的系统化路径Dream Cosmos11 小时前
传输层协议之UDP梦到啥说啥13 小时前
Linux网络编程:UDP网络协议FungLeo15 小时前
成为全栈·Vue 管理后台篇·用 fetch 建一层可控的请求内核张小姐的猫16 小时前
【基于 WebSocket 的多用户网页五子棋】—— 项目结构设计与实用工具类模块wuyk5551 天前
Spider-第03讲-HTTP速通与读懂响应梦到啥说啥2 天前
Linux网络编程:TCP网络协议Jeremy_WW2 天前
802.3协议解读 06:117章节 RS 和 200GMII / 400GMII I杨凯凡2 天前
Spring Framework 7 HTTP Interface 实战:声明式 RestClient、超时与测试沫璃染墨2 天前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制