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());
    }
}
相关推荐
进击的荆棘8 小时前
Linux网络——应用层协议HTTP
linux·运维·服务器·网络·http
步峰代理-盾山10 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
王亚博_Abel1 天前
域名是怎么变成 IP 的?手拆 DNS 解析四个实验实测
网络·网络协议·tcp/ip
用户84913717547161 天前
两版 SNMP 报文只差 1 个字节,我盯了半天才看懂问题出在哪
网络协议
AndreaYYmm1 天前
SIGUSR2 触发热重载:一个正在抓包的进程如何原子地换掉规则集
c语言·网络协议
Thanks_ks1 天前
RESTful API 到底是什么:从 URL 设计讲到请求方法的选择
spring boot·http·状态码·url·幂等性·restful api·无状态
用户84913717547161 天前
为什么 SNMP 叫"简单"?真相是:设备少干活,复杂全甩给了你
网络协议
白小飞不会飞1 天前
【AUTOSAR】CP SOME/IP-TP
c++·嵌入式硬件·网络协议·车载系统·autosar
神一样的老师1 天前
用 FCU1501 双网口打通两个不同网段:IP 转发 + NAT 实战
网络·网络协议·tcp/ip
keepquietl2 天前
UDS on CAN 入门(6):诊断响应要等多久?读懂 P2、P2* 和 S3
网络·网络协议