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
相关推荐
qq_359716232 小时前
UDP的使用搞全栈小苏3 小时前
基于Qt QML和C++的MQTT测试客户端(CMakeLists实现)提笔忘字的帝国5 小时前
宝塔SSL自动续签上海云盾商务经理杨杨5 小时前
高防IP如何抵御CC攻击?2025年全面防护机制解析李白你好5 小时前
Ping命令为何选择ICMP而非TCP/UDP?high20117 小时前
【 运维相关】-- HTTP 压测/负载发生器之新秀 ohaAD钙奶-lalala9 小时前
HTTP response code 200 206 416详解sun03229 小时前
使用 javax.net.ssl.HttpsURLConnection 发送 HTTP 请求_以及为了JWT通信选用OSS的Jar的【坑】fatiaozhang952711 小时前
中国移动云电脑一体机-创维LB2004_瑞芯微RK3566_2G+32G_开启ADB ROOT安卓固件-方法3蒋星熠11 小时前
WebSocket网络编程深度实践:从协议原理到生产级应用