Http请求Body中携带raw XML形式

复制代码
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());
    }
}
相关推荐
沫璃染墨4 小时前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组
运维·服务器·网络·网络协议·tcp/ip·ip
大今野16 小时前
第 16 章计算机网络体系结构详解:OSI七层模型、TCP/IP四层协议、三次握手与四次挥手
网络协议·tcp/ip·php·专转本
LabVIEW开发1 天前
Ethernet/IP 会话无效 -251723770
服务器·网络协议·tcp/ip
nice_lcj5201 天前
【从零理解计算机网络 · 15】完整串联:一次请求的全旅程,以及那道经典面试题
网络·计算机网络·http·面试·tcp·总结·网络排查
xbzb1 天前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
光电笑映2 天前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(下)
linux·网络·网络协议·tcp
code2cat2 天前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
SemiTris2 天前
Web 前端底层起源:HTML/CSS/JS、浏览器与 HTTP 完整脉络
http·html
修仙之路2 天前
2.1 HTTP 协议核心:请求响应模型、状态码、Header 与 RESTful 设计
网络协议·http
ManageEngine卓豪2 天前
IP 冲突与端口乱接:从 ARP 排障到 IPAM 落地实操
网络协议·tcp/ip·ip地址管理·ipam