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());
    }
}
相关推荐
Shell运维手记2 小时前
ARP 协议超详细讲解(适合网工考试 / 运维理解)
运维·网络·网络协议·智能路由器
huainingning4 小时前
华三瘦AP切换为胖AP并配置无线功能
运维·网络·网络协议·tcp/ip
减瓦6 小时前
告别Postman——用VSCode优雅地发起Http请求
vscode·http·postman
跨境技工小黎7 小时前
4G/5G 移动代理实战:什么时候必须用移动 IP?
网络协议·tcp/ip·5g
wechatbot8887 小时前
解决企业微信官方 API 短板:iPad 协议全功能对接方案
java·汇编·windows·http·微信·企业微信
米尔的可达鸭8 小时前
UDP 通信深度实验:从 Windows FastPath 到 ICMP 端口不可达的完整排查
windows·websocket·网络协议·rust·udp·wireshark·tcpdump
Dxy12393102168 小时前
Python 实现POST上行压缩上传可用HTTP库汇总
开发语言·python·http
午安~婉8 小时前
git中http与ssh连接
git·http·ssh
Full Stack Developme9 小时前
Tomcat 如何处理HTTP请求
java·http·tomcat
味悲10 小时前
Apache HTTP 服务器配置
服务器·http·apache