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());
    }
}
相关推荐
拾忆,想起21 小时前
Dubbo配置方式大全:七种配置任你选,轻松玩转微服务!
服务器·网络·网络协议·微服务·云原生·架构·dubbo
jinxinyuuuus1 天前
文件格式转换工具:XLSX的XML流式解析、数据类型推断与CSV编码规范
xml
xinxinhenmeihao1 天前
爬虫如何使用代理IP才能不被封号?有什么解决方案?
爬虫·网络协议·tcp/ip
Moonquake_www1 天前
WSL2设置桥接网络至主机IP
网络·网络协议·tcp/ip
苏小瀚1 天前
[JavaSE] 网络原理(HTTP_HTTPS)
网络·tcp/ip·http
HIT_Weston1 天前
51、【Ubuntu】【Gitlab】拉出内网 Web 服务:http.server 单/多线程分析(三)
ubuntu·http·gitlab
杜大哥1 天前
电脑:如何解决电脑网络IP地址冲突?
网络协议·tcp/ip·电脑
cui_win1 天前
HTTP协议:常见状态码(400/500 系列)
网络·网络协议·http
北京耐用通信1 天前
工业通信升级利器:耐达讯自动化Ethernet/IP转CC-Link网关让IO模块兼容无忧!
网络·人工智能·科技·物联网·网络协议·自动化·信息与通信
刘孬孬沉迷学习1 天前
SCTP、GTP-U 和 WebSocket 协议比较
网络·websocket·网络协议