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
相关推荐
2401_8734794015 分钟前
IP地址位置精确查询的原理是怎样的?从城市级到街道级的技术拆解深念Y1 小时前
老Flyme 官方 root 开启 ADB TCP 方案发光小北13 小时前
PLC 私有协议采集网关如何应用?00后程序员张18 小时前
抓包鹰 电脑本机网络连接表怎么看?哪些程序在联网 进程归属与可疑 IP 追溯2401_8685347821 小时前
第二十章|微服务系统分析与设计(上)微微1笑抽了筋1 天前
xml中设置透明度DsirNg1 天前
一文讲清网络协议:从打开网页到理解 HTTP、HTTPS 与 TLS奈斯先生Vector1 天前
2026 AIGC API 网关韧性评测:为什么 HTTP 200 不等于有效交付拾光Ծ1 天前
【Linux网络】深入理解网络层:从IP协议格式,子网划分到NAT与路由机制谏书稀1 天前
C++/Python 混合编程(CPython)中 GIL饥饿 导致的 HTTP 服务无响应问题与解决方案