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());
    }
}
相关推荐
Moyu_Friday12 小时前
网络安全基础-计算机网络应用与协议
网络协议·计算机网络
徐同保1 天前
免费ssl证书全流程AI配置(含七牛云cdn)
网络·网络协议·ssl
幻智星科技1 天前
一个开源的纯FPGA网口采集卡
网络协议·fpga开发·udp
flash俊杰1 天前
给工具调用划安全红线:SSRF 防护、逐跳复检与沙箱取舍
http
anew___1 天前
《从零手写操作系统 (48):网络协议栈基础——Socket API与TCP/IP分层架构》
网络协议·tcp/ip·架构
TMT星球1 天前
2026阅文创作大会:首批AI精品动画亮相,推出OPC一站式平台与IP委员会模式
人工智能·网络协议·tcp/ip
泡海椒1 天前
从 XML 到 Excel:JQuick-Excel 代理与解析器如何协作
xml·excel
程序员夏洛1 天前
TCP 和 UDP 有什么区别?
网络协议·tcp/ip·udp