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());
    }
}
相关推荐
chenyuhao202414 小时前
Linux网络编程:HTTP协议
linux·服务器·网络·c++·后端·http·https
duration~14 小时前
IPv6 详解
网络·网络协议·ip
福尔摩斯张16 小时前
Linux的pthread_self函数详解:多线程编程中的身份标识器(超详细)
linux·运维·服务器·网络·网络协议·tcp/ip·php
代码游侠17 小时前
复习——网络基础知识
网络·笔记·网络协议·算法·http
wregjru17 小时前
【C++】2.4 map和set的使用
网络·网络协议·rpc
计算机小手18 小时前
Kong + Konga 网关入门实践:Docker 部署、反向代理与插件使用指南
运维·经验分享·网络协议·docker·kong·开源软件
博语小屋19 小时前
TCP:协议、序列化与反序列化、JSON 数据和jsoncpp
linux·网络·网络协议·tcp/ip·json
不染尘.21 小时前
cookie和session技术及实现
服务器·网络·网络协议·计算机网络
大连好光景21 小时前
socket.socket模块--网络通信
网络·python·网络协议
就叫飞六吧21 小时前
wrk:现代 HTTP 性能测试工具(类cc)
网络协议·测试工具·http