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());
    }
}
相关推荐
汤愈韬8 小时前
NAT Server 与目的Nat
网络·网络协议·网络安全·security
7ACE9 小时前
Wireshark TS | TLP 超时时间
网络·网络协议·tcp/ip·wireshark·tcpdump
凯瑟琳.奥古斯特14 小时前
NAT原理及作用详解
网络·网络协议
W.A委员会16 小时前
DNS详解
http
_Evan_Yao17 小时前
从 IP 路由到 Agent 路由:最长前缀匹配如何帮你分发任务?
java·网络·后端·网络协议·tcp/ip
数据法师1 天前
开源情报收集工具GhostTrack深度测评:IP、手机号、用户名的合规信息查询方案
网络·网络协议·tcp/ip
想成为优秀工程师的爸爸1 天前
第三十篇技术笔记:郭大侠学UDS - 人有生老三千疾,望闻问切良方医
网络·笔记·网络协议·tcp/ip·信息与通信
灰子学技术1 天前
Envoy HTTP Connection Manager (HCM) 技术文档
网络·网络协议·http
日取其半万世不竭2 天前
WordPress建站 + 免费SSL证书配置完整教程
网络·网络协议·ssl
七七powerful2 天前
AI+运维提效--HTTPS 证书有效期监控方案
网络协议·http·https