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());
    }
}
相关推荐
凌云C11 小时前
5G 协议体系与交互流程
网络协议
X7x516 小时前
跨厂商路由引入(重发布)实战指南:华为、华三、思科的逻辑差异与配置避坑
网络协议·信息与通信·信号处理·实施路由引入
猫头_17 小时前
AI 流式传输工程指南:有了 EventSource 为何还要 Fetch?
javascript·http·llm
玩转4G物联网17 小时前
FS800DTU 新品上线|免费使用 DMP 设备管理平台,支持远程批量配置 & OTA 升级
物联网·网络协议·tcp/ip·云平台·核心板·fs800dtu·iot管理平台
Chloeis Syntax17 小时前
JAVAEE初阶 --- 构造HTTP请求
网络·网络协议·http·postman
风起洛阳@不良使18 小时前
spring中xml和注解开发的对比
xml·java·spring
wuqingshun31415918 小时前
说说TCP的3次握手?为什么需要三次握手?
服务器·网络协议·tcp/ip
无锡耐特森19 小时前
解决网关数据异常EtherNetIP 转Modbus TCP专用动态监测
网络·网络协议·tcp/ip
2401_8734794020 小时前
爬虫IP怎么防?IP离线库四层识别+日志留存,反爬留证两不误
爬虫·网络协议·tcp/ip·ip
超级赛博搬砖工21 小时前
什么是移动代理IP?与住宅IP有何区别?
网络·网络协议·tcp/ip