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());
    }
}
相关推荐
缪懿9 小时前
应用层中的UDP协议原理
网络·网络协议·udp·javaee
汤愈韬10 小时前
TK_HCIP-Security_FW的可靠性_双机热备场景_上接路由器下接交换机
网络·网络协议·网络安全
野熊佩骑14 小时前
一文读懂Nginx 之 Ubuntu使用apt方式安装Nginx官方最新版本
linux·运维·服务器·nginx·ubuntu·http
TechWayfarer15 小时前
出海APP本地化实战:基于IP归属地API的网关路由与多语言自动切换方案
网络·python·网络协议·tcp/ip
却道天凉_好个秋15 小时前
云中网络:GRE
网络协议·gre
带娃的IT创业者15 小时前
当隐私守护者成为指纹:深度解析 Mullvad 出口 IP 的“反向识别”陷阱
网络·网络协议·tcp/ip·vpn·指纹识别·隐私保护·mullvad
雪度娃娃15 小时前
Asio——socket的创建和连接
linux·运维·服务器·c++·网络协议
川石课堂软件测试16 小时前
接口测试常见面试题及答案
python·网络协议·mysql·华为·单元测试·prometheus·harmonyos
minji...17 小时前
Linux 网络基础之传输层TCP(七)确认应答机制,超时重传机制,连接管理机制(三次握手四次挥手),流量控制,滑动窗口,快重传
linux·运维·服务器·网络·网络协议·tcp/ip·http
专吃海绵宝宝菠萝屋的派大星18 小时前
spring Ai 开发的mcp-由sse改成Streamable HTTP
人工智能·spring·http