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());
    }
}
相关推荐
学习中的DGR6 小时前
[极客大挑战 2019]Http 1 新手解题过程
网络·python·网络协议·安全·http
hjhcos8 小时前
【宝塔】局域网IP申请SSL证书,解决浏览器本地环境可以访问摄像头,发布环境不能访问摄像头的问题
网络协议·tcp/ip·ssl
Gensors传感器10 小时前
Gensors解读:TCP/IP协议在压力扫描系统中的作用详解
网络·网络协议·tcp/ip·压力测试·压力扫描阀·扫描阀
我送炭你添花10 小时前
树莓派部署 GenieACS 作为终端TR-069 ACS(自动配置服务器)的详细规划方案
运维·服务器·网络协议
码农娟10 小时前
Hutool XML工具-XmlUtil的使用
xml·java
LuminescenceJ10 小时前
GoEdge 开源CDN 架构设计与工作原理分析
分布式·后端·网络协议·网络安全·rpc·开源·信息与通信
我在人间贩卖青春11 小时前
UDP协议
网络·网络协议·udp
卓码软件测评12 小时前
【第三方软件测试测评机构:使用LoadRunner测试HTTPS/SSL协议应用的配置和证书处理 】
网络协议·测试工具·https·测试用例·ssl
七夜zippoe12 小时前
gRPC高性能RPC框架实战:从Protocol Buffers到流式传输的完整指南
网络·python·网络协议·rpc·protocol
一路往蓝-Anbo12 小时前
第 1 篇:对象池模式 (Object Pool) —— 裸机下的动态内存革命
jvm·数据库·stm32·单片机·嵌入式硬件·网络协议·tcp/ip