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());
    }
}
相关推荐
TechWayfarer1 小时前
知乎/微博的IP属地显示为什么偶尔错误?用IP归属地查询平台自检工具3步验证
网络·python·网络协议·tcp/ip·网络安全
孙同学_7 小时前
【项目篇】高并发服务器 - HTTP服务器组件拆解,从Util到HttpServer
运维·服务器·http
SilentSamsara8 小时前
HTTP/1.1 到 HTTP/3:每代协议解决了什么问题
网络·网络协议·tcp/ip·http·https
Flash.kkl9 小时前
传输层UDP、TCP
网络协议·tcp/ip·udp
zmj32032410 小时前
TCP/IP协议和以太网关系
网络·网络协议·tcp/ip
chao18984412 小时前
Socket-TCP 简易端口开放检测工具
网络·网络协议·tcp/ip
不会写DN12 小时前
TCP 长连接服务:登录注册认证体系实战指南
服务器·网络·网络协议·tcp/ip·计算机网络·面试
一只小鱼儿吖13 小时前
基于OpenClaw的代理IP池自动化监控方案
网络协议·tcp/ip·自动化
蕤葳-13 小时前
理性分析:如何利用考证作为抓手,构建系统化知识体系与职业规划?
人工智能·网络协议·https
通信小呆呆14 小时前
各具神通——Vivado中不同系列的IP核差异详解
网络协议·tcp/ip·fpga开发