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
相关推荐
拾忆,想起21 小时前
Dubbo配置方式大全:七种配置任你选,轻松玩转微服务!jinxinyuuuus1 天前
文件格式转换工具:XLSX的XML流式解析、数据类型推断与CSV编码规范xinxinhenmeihao1 天前
爬虫如何使用代理IP才能不被封号?有什么解决方案?Moonquake_www1 天前
WSL2设置桥接网络至主机IP苏小瀚1 天前
[JavaSE] 网络原理(HTTP_HTTPS)HIT_Weston1 天前
51、【Ubuntu】【Gitlab】拉出内网 Web 服务:http.server 单/多线程分析(三)杜大哥1 天前
电脑:如何解决电脑网络IP地址冲突?cui_win1 天前
HTTP协议:常见状态码(400/500 系列)北京耐用通信1 天前
工业通信升级利器:耐达讯自动化Ethernet/IP转CC-Link网关让IO模块兼容无忧!刘孬孬沉迷学习1 天前
SCTP、GTP-U 和 WebSocket 协议比较