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());
    }
}
相关推荐
工控小楠2 小时前
DeviceNet转Modbus TCP网关的远程遥控接收端连接研究
网络·网络协议·devicenet·profient
midsummer_woo4 小时前
【2025年】解决Burpsuite抓不到https包的问题
网络协议·http·https
光芒Shine5 小时前
【物联网-TCP/IP】
网络·网络协议·tcp/ip
小白杨树树7 小时前
【WebSocket】SpringBoot项目中使用WebSocket
spring boot·websocket·网络协议
云计算-Security7 小时前
如何理解 IP 数据报中的 TTL?
网络协议·tcp/ip
itachi-uchiha8 小时前
命令行以TLS/SSL显式加密方式访问FTP服务器
服务器·网络协议·ssl
稳联技术9 小时前
实践提炼,EtherNet/IP转PROFINET网关实现乳企数字化工厂增效
网络·网络协议·tcp/ip
Icoolkj9 小时前
WebRTC 与 WebSocket 的关联关系
websocket·网络协议·webrtc
红米饭配南瓜汤9 小时前
WebRTC中的几个Rtp*Sender
网络·网络协议·音视频·webrtc·媒体
猫头虎12 小时前
[特殊字符]解决 “IDEA 登录失败。不支持早于 14.0 的 GitLab 版本” 问题的几种方法
java·ide·网络协议·http·https·gitlab·intellij-idea