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());
    }
}
相关推荐
吴建旭 智宅焕7 小时前
智能家居全国交付中执行端能力跃迁的架构设计:从标准装调到交付专家的系统化路径
网络·网络协议·智能家居
Dream Cosmos11 小时前
传输层协议之UDP
网络·网络协议·udp
梦到啥说啥13 小时前
Linux网络编程:UDP网络协议
linux·网络协议·udp
FungLeo15 小时前
成为全栈·Vue 管理后台篇·用 fetch 建一层可控的请求内核
前端·javascript·vue.js·http·fetch·formdata·接口封装
张小姐的猫16 小时前
【基于 WebSocket 的多用户网页五子棋】—— 项目结构设计与实用工具类模块
网络·websocket·网络协议
wuyk5551 天前
Spider-第03讲-HTTP速通与读懂响应
网络·爬虫·网络协议·http
梦到啥说啥2 天前
Linux网络编程:TCP网络协议
网络·网络协议·tcp/ip
Jeremy_WW2 天前
802.3协议解读 06:117章节 RS 和 200GMII / 400GMII I
网络·网络协议·信息与通信·光模块·802.3协议
杨凯凡2 天前
Spring Framework 7 HTTP Interface 实战:声明式 RestClient、超时与测试
java·spring·http
沫璃染墨2 天前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制
运维·服务器·网络·c++·网络协议·tcp/ip·tcp