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
相关推荐
静心观复9 小时前
从短连接到 gRPC:一文读懂 HTTP 连接模型的演进w1wi11 小时前
【Vibe Coding】TCP/UDP包篡改重放工具treesforest11 小时前
IP地址段查询完全指南:从单IP查到IPv4段批量归属地查询wangl_9211 小时前
Modbus RTU 与 Modbus TCP 深入指南-字节顺序与跨平台问题wefg112 小时前
【计算机网络】DNS/ICMP协议/ping指令花间相见12 小时前
【全栈开发03】—— curl 常用参数详解与 HTTP 请求实战S1998_1997111609•X13 小时前
哈希树函数洪水泛滥污染孪生镜像导致生物量子信息泄露以钩子而爬虫植入ssd探测原来是猿14 小时前
应用层【协议再识/序列化与反序列化】天草二十六_简村人14 小时前
对接AI大模型之nginx代理配置SSE接口报错小能手15 小时前
HTTP/2 牛逼在哪?