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());
    }
}
相关推荐
凌波粒9 分钟前
什么是 MCP(模型上下文协议)
人工智能·网络协议·aigc
Ether IC Verifier2 小时前
TCP/IP四层协议详解与以太网包发送过程
网络·网络协议·tcp/ip·计算机网络·dpu
Summer不秃4 小时前
深入理解 Token 无感刷新:从并发雪崩到单例锁 + 请求队列的完整实现
前端·http
Ether IC Verifier5 小时前
CPU/GPU/NPU/DPU功能详解与AI应用分析
网络·人工智能·网络协议·tcp/ip·计算机网络·dpu
lunzi_08268 小时前
《图解HTTP》--第3章 HTTP报文内的HTTP信息
网络·网络协议·http
只可远观9 小时前
Android XML命令式和Jetpack Compose声明式UI
android·xml
Shota Kishi10 小时前
ERPC 在 Solana RPC 中集成 Pyth Hermes 兼容的 Price API:从架构到调用的技术解析
网络协议·rpc·架构
念越10 小时前
HTTPS 安全内核:对称与非对称加密的博弈,数字证书一战定局
java·网络·网络协议·安全·https
Jinkxs11 小时前
LoadBalancer- 核心术语详解:转发 / 监听 / 节点池 / 虚拟 IP 等必知概念
网络·网络协议·tcp/ip
运维行者_11 小时前
理解应用性能监控
大数据·服务器·网络·数据库·人工智能·网络协议·安全