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());
    }
}
相关推荐
HMS工业网络3 小时前
如何解决使用TwinCAT时EtherCAT网络出现“Sync Manager Watchdog”报错
网络·网络协议·网络安全
审判长烧鸡7 小时前
标准 HTTP API 签名鉴权 Header 完整规范
http·web
w1wi8 小时前
安卓抓包完全指南(一):从入门到 SSL Pinning 绕过
android·网络协议·ssl
刘马想放假11 小时前
OpenVPN 深度解析:从协议原理到生产实践
运维·网络协议
00后程序员张12 小时前
HTTPS单向认证、双向认证、抓包原理与反抓包策略详解
网络协议·http·ios·小程序·https·uni-app·iphone
June bug12 小时前
Failed to fetch+HTTP 422=Agent ID 不匹配
网络·网络协议·http
minji...15 小时前
Linux 网络基础之传输层协议TCP(九)从内核源码的角度打通系统与网络之间的关系,套接字多态的体现
linux·运维·服务器·网络·网络协议·tcp/ip·http
灰子学技术16 小时前
Envoy IP 标签(IP Tagging)功能实现分析
网络·网络协议·tcp/ip
想唱rap16 小时前
IO多路转接之epoll
linux·运维·服务器·数据库·网络协议·算法·http
初願致夕霞16 小时前
Linux网络编程_数据链路层MAC帧协议与ARP协议
linux·网络·网络协议·macos