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());
    }
}
相关推荐
还下着雨ZG20 小时前
TCP/IP协议族详细介绍
网络·网络协议·tcp/ip·计算机网络
記億揺晃着的那天1 天前
WebSocket 通俗讲解
网络·websocket·网络协议·实时通信
xxtzaaa1 天前
手游端游IP被限制无法多开如何解决
网络协议·tcp/ip·智能路由器
听风吟丶1 天前
Java 11+ HttpClient 实战:从 HttpURLConnection 到现代 HTTP 客户端的全面升级
java·开发语言·http
小草cys1 天前
【解决】华为欧拉系统上遇到能 ping 通 IP 地址(如 8.8.8.8)但无法 ping 通域名(如 www.baidu.com)的情况
网络·网络协议·tcp/ip
重启编程之路1 天前
python 基础学习socket -UDP编程
python·网络协议·学习·udp
站长朋友1 天前
解决SSL证书安装后网站仍显示“不安全”的问题
网络协议·安全·ssl·ssl证书安装不安全·锐安信ssltrus·ocsp响应速度·根证书链完整
踏浪无痕1 天前
记一次被 K8s 网络 SNAT 坑惨的经历
网络协议·kubernetes
百***37481 天前
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
xml·spring·mybatis