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());
    }
}
相关推荐
YumiProxy16 小时前
流媒体区域内容访问:代理IP的技术原理、检测机制与稳定性优化
服务器·网络·网络协议·tcp/ip·ip
流烟默18 小时前
Spring Boot 异常响应被序列化为 XML 的排查与修复
xml·spring boot·后端
mmsx18 小时前
Android 地图上绘制 KML:解析、样式与渲染
android·xml
青瓦梦滋20 小时前
【在线五子棋对战】游戏匹配模块
网络·c++·网络协议·http·https
不会C语言的男孩21 小时前
libxml2 源码编译与 XML 读取验证(x86 / ARM)
xml·c语言·嵌入式
zxanz121 小时前
一张 SSL 证书,如何降低业务流量劫持与数据泄露风险
网络·网络协议·ssl
Novproxy_Z21 小时前
爬虫任务被拦截:区分 IP 封禁、设备封禁、账号封禁
爬虫·网络协议·tcp/ip
隐擎fox1 天前
解密 BGP Anycast 边缘路由与跨国链路质量检测:多租户网络出口的低时延与防丢包架构实战
网络·python·网络协议·tcp/ip·网络安全·架构·tcpdump
聚美智数1 天前
网站HTTP状态查询-SEO优化
网络·网络协议·http
是真的小外套1 天前
第9章:网络层安全协议 IPSec 学习笔记
网络·网络协议·tcp/ip