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());
    }
}
相关推荐
captain3766 小时前
网络原理(3)-TCP核心机制连接管理▲▲▲
java·服务器·网络·ide·网络协议·tcp/ip·java-ee
易岳群6 小时前
搞清 Socket、TCP、MQTT 的通信关系:从底层协议到物联网实战
网络协议·tcp/ip·socket·通信协议关系
宵时待雨7 小时前
linux笔记归纳19:网络层协议IP
linux·网络·笔记·网络协议·tcp/ip
只睡四小时7 小时前
内网穿透+WebSocket:Node 零依赖远程桌面实战
网络·websocket·网络协议
Jeremy_WW7 小时前
802.3协议解读 05:116章节 200 Gb/s 和 400 Gb/s 网络介绍 V
网络·网络协议·信息与通信·光模块·802.3协议
梦想平凡8 小时前
百游棋牌源代码开发搭建教程(七):WebSocket房间同步与断线恢复
网络·websocket·网络协议
Lhappy嘻嘻9 小时前
网络(六)|应用层 HTTP&HTTPS:URL、请求响应、Cookie 与证书
网络·http·https
筝筝ba10 小时前
如何跳过FFDC
xml·linux·运维·服务器·网络
tachibana211 小时前
WebSocket 和 SSE 通信的区别及局限性
网络·人工智能·websocket·网络协议·ai·llm·agent
为思念酝酿的痛12 小时前
Linux网络进程守护
网络协议·tcp/ip·udp