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 小时前
从短连接到 gRPC:一文读懂 HTTP 连接模型的演进
网络·网络协议·http
w1wi11 小时前
【Vibe Coding】TCP/UDP包篡改重放工具
人工智能·网络协议·tcp/ip·ai·udp·ai编程
treesforest11 小时前
IP地址段查询完全指南:从单IP查到IPv4段批量归属地查询
网络·数据库·网络协议·tcp/ip·网络安全·运维开发
wangl_9211 小时前
Modbus RTU 与 Modbus TCP 深入指南-字节顺序与跨平台问题
网络·网络协议·tcp/ip·tcp·modbus·rtu
wefg112 小时前
【计算机网络】DNS/ICMP协议/ping指令
网络·网络协议·计算机网络
花间相见12 小时前
【全栈开发03】—— curl 常用参数详解与 HTTP 请求实战
网络·网络协议·http
S1998_1997111609•X13 小时前
哈希树函数洪水泛滥污染孪生镜像导致生物量子信息泄露以钩子而爬虫植入ssd探测
爬虫·网络协议·缓存·哈希算法·开闭原则
原来是猿14 小时前
应用层【协议再识/序列化与反序列化】
linux·运维·服务器·网络·网络协议·tcp/ip
天草二十六_简村人14 小时前
对接AI大模型之nginx代理配置SSE接口
运维·网络·nginx·http·阿里云·ai·云计算
报错小能手15 小时前
HTTP/2 牛逼在哪?
网络·网络协议·tcp/ip·http