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());
    }
}
相关推荐
hellojackjiang20116 小时前
socket长连接在手游场景下的技术实践
网络·网络协议·tcp/ip·架构·网络编程
wangl_927 小时前
Modbus RTU 与 Modbus TCP 深入指南-安全加固方案
网络·网络协议·tcp/ip·安全·tcp·modbus·rtu
源远流长jerry8 小时前
Linux 网络性能优化:从应用到内核
linux·运维·服务器·网络·网络协议·性能优化
落叶_Jim8 小时前
Let‘s Encrypt证书有效期缩短至90天后,如何实现自动续期
网络协议·https·ssl
Shingmc310 小时前
【Linux】应用层协议HTTP
网络·网络协议·http
ZC跨境爬虫10 小时前
跟着 MDN 学 HTML day_34:(深入XML 中的 CDATASection 接口)
xml·前端·html·html5·媒体
加号311 小时前
【C#】 HTTP 请求通讯实现指南
开发语言·http·c#
wangl_9211 小时前
Modbus RTU 与 Modbus TCP 深入指南-串口服务器:RTU转TCP
服务器·网络·网络协议·tcp/ip·tcp·modbus·rtu
七七powerful11 小时前
AI+运维提效,ssl-cert-monitoring(SSL证书监控系统)2.0开发完毕
运维·网络协议·ssl
CDN36011 小时前
告别TCP队头阻塞!HTTP/3与QUIC协议在2026年的实战落地
网络协议·tcp/ip·http