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());
    }
}
相关推荐
李白的天不白24 分钟前
http https
网络协议·http·https
就叫_这个吧14 小时前
IDEA Mybatis xml文件,实现sql语句联想,自动填入补充
xml·mysql·intellij-idea·mybatis
上海云盾-小余14 小时前
源站隐藏实战:规避裸 IP 被直接攻击的完整方案
数据库·网络协议·tcp/ip
johnny23314 小时前
WebDAV概述、原理、拓展:SMB、ZeroByte
网络协议
代码中介商15 小时前
HTTP进化史:从1.0到3.0的核心变革
网络·网络协议·http
范什么特西17 小时前
配置文件xml和properties
xml·前端
sweet丶18 小时前
MQTT在iOS中的应用实践
网络协议
ALINX技术博客18 小时前
【黑金云课堂】FPGA技术教程Vitis开发:TCP以太网通信
网络协议·tcp/ip·fpga开发
matrixmind819 小时前
HTTPX:Python 下一代 HTTP 客户端
python·其他·http·httpx
W.W.H.20 小时前
Ping 与 TCP:网络连通性探测的两种维度
网络·网络协议·tcp/ip