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());
    }
}
相关推荐
bbq粉刷匠3 分钟前
HTTP 深度解析
网络·网络协议·http
tiantianuser18 分钟前
NVME-oF IP 设计16 : 适于高速网络存储系统的IP设计2
网络协议·rdma·高速传输·roce v2·nvme of
FPGA小迷弟2 小时前
AXI4-Stream协议详解与Vivado自定义IP核封装实战
网络协议·tcp/ip·fpga开发·fpga·硬件开发
ControlRookie18 小时前
第15篇_Client 04|GET、POST 和 Body 怎样保持一致
http·codesys·plc通信·controlrookie
LitchiCheng20 小时前
还在改xml调相机?MuJoCo交互式可视化调整相机安装位置
xml·数码相机
啦啦啦啦啦zzzz1 天前
TCP协议详解
网络·网络协议·tcp/ip
张小姐的猫1 天前
【Linux】网络编程 —— 传输层协议 TCP(下)
linux·运维·服务器·网络·tcp/ip·http·php
Little Tian1 天前
基于FPGA的UDP回环实验(二)----ARP模块
网络·网络协议·udp
K成长日志1 天前
BLE链路层--比特流处理
网络·物联网·网络协议·蓝牙·iot·ble·无线
richard_yuu1 天前
JSON vs XML vs 二进制:3种序列化终极选型
xml·c++·qt·学习·json