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());
    }
}
相关推荐
福尔摩斯张6 小时前
插件式架构:解耦与扩展的艺术与实践(超详细)
linux·服务器·网络·网络协议·tcp/ip
FserSuN8 小时前
TCP RST 与 Broken Pipe:从协议语义到操作系统信号的体系化梳理
网络·网络协议·tcp/ip
apihz9 小时前
全球IP归属地查询免费API详细指南
android·服务器·网络·网络协议·tcp/ip
fiveym9 小时前
PXE装机遇DHCP错误:ICMP echo reply导致IP分配失败原理解析
网络协议·tcp/ip·php
梁辰兴10 小时前
计算机网络基础:局域网的数据链路层
网络·网络协议·计算机网络·计算机·数据链路层·局域网·梁辰兴
老王熬夜敲代码10 小时前
IP和MAC的深入理解
linux·网络·笔记·网络协议
小熊哥^--^10 小时前
谈谈对传输层协议TCP的理解
网络·网络协议·tcp/ip
缺的不是资料,是学习的心10 小时前
vmware虚拟机ens33拿不到ip,已经开启dhcp了
网络·网络协议·tcp/ip
Dovis(誓平步青云)11 小时前
《Linux生态下HTTP协议解析+进阶HTTPS证书:抓包、拆解与问题排查实战》
linux·运维·http
Boop_wu11 小时前
[Java EE] 网络原理(2) http
网络·网络协议·http