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());
    }
}
相关推荐
nvd1110 分钟前
GCP 7层 外部 HTTP 负载均衡器深度解析
网络协议·http·负载均衡
Bruce_Liuxiaowei13 小时前
轻量级 IOC 管理方案:Streamlit 驱动的恶意IP情报平台
网络·jvm·网络协议·tcp/ip·安全
云水一下19 小时前
零基础玩转bWAPP靶场(三十):XML/XPath 注入(登录表单)
xml·web安全·bwapp
雾里0不看花21 小时前
.NET通过HTTP操作MINIO
http·.net·iphone
云水一下1 天前
零基础玩转bWAPP靶场(三十一):XML/XPath 注入(搜索)
xml·web安全·bwapp
AI人工智能+电脑小能手1 天前
【大白话说Java面试题 第210题】【10_网络协议篇】第1题:说说 TCP/IP 网络五层模型
java·网络协议·tcp/ip·计算机网络·网络五层模型
不在逃避q1 天前
使用.NET实现自带思考的Tool 并且提供mcp streamable http服务
网络协议·http·.net
K成长日志1 天前
BLE链路层空口包--数据物理信道PDU
网络·物联网·网络协议·嵌入式·蓝牙·iot·ble
一条泥憨鱼1 天前
【从0开始学习计算机网络】| HTTP方法疑点解析
开发语言·计算机网络·http
一条泥憨鱼1 天前
【从0开始学习计算机网络】| RESTful API 设计原则
计算机网络·http·restful api