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());
    }
}
相关推荐
handsomestWei5 小时前
Docker引擎API接入配置
运维·http·docker·容器·api
Mike_6665 小时前
txt_json和xml_json
xml·python·json
20YC编程社区5 小时前
一分钟了解XML语言格式,使用场景,和它的优缺点
xml
Kay.Wen9 小时前
LabVIEW 转换 XML文本
xml·labview
汤愈韬10 小时前
网络安全之网络基础知识_2
网络协议·安全·web安全
风逸尘_lz12 小时前
05-LPB3568针对不同网段实现UDP通信
网络·网络协议·udp
灰子学技术14 小时前
Envoy 中 TCP 网络连接实现分析
运维·服务器·网络·网络协议·tcp/ip
IpdataCloud14 小时前
米哈游黑产案解析:游戏账号批量注册如何用IP查询识别外挂与多开用户?操作指南
网络协议·tcp/ip·游戏
星宇笔记15 小时前
我做了一个本地网页版嵌入式调试工具:支持 Serial / TCP / UDP、实时曲线、HEX 发送
单片机·嵌入式硬件·网络协议·tcp/ip·udp·开源软件
练习时长一年15 小时前
浅谈assembly插件打包分发机制
xml