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());
    }
}
相关推荐
科技块儿11 分钟前
反羊毛实战复盘:IP查询+设备指纹联动风控方案
网络·网络协议·tcp/ip
掘根1 小时前
【仿Muduo库项目】HTTP模块3——HttpContext子模块
网络·网络协议·http
瓦尔登湖懒羊羊1 小时前
到底能不能把HTTP1.0讲明白了
http
小李独爱秋2 小时前
计算机网络经典问题透视:TLS协议工作过程全景解析
运维·服务器·开发语言·网络协议·计算机网络·php
不穿格子的程序员3 小时前
计算机网络篇1:OSI + HTTP进化史 + 三次握手四次挥手
网络协议·计算机网络·http
常温5104 小时前
关于网卡如何给CPU减负方法及策略
网络协议·tcp/ip·udp
莫小墨4 小时前
Modbus协议
网络协议
汤愈韬4 小时前
防火墙双机热备技术之VRRP
网络·网络协议·网络安全·security·huawei
北京耐用通信4 小时前
告别通信干扰与距离限制:耐达讯自动化Profibus总线光纤中继器赋能伺服驱动器稳定连接
人工智能·科技·网络协议·自动化·信息与通信
乾元5 小时前
10 个可复制的企业级项目:从需求到交付的 AI 网络工程模板(深度实战版)
运维·网络·人工智能·网络协议·安全