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());
    }
}
相关推荐
辛迪聊物业数字化12 小时前
物业费收缴率从68%提升到97%落地复盘:中小物业收费系统选型避坑指南
人工智能·嵌入式硬件·物联网·网络协议·系统架构
80s77712 小时前
态住宅IP在跨境电商防关联中的技术应用
网络·网络协议·tcp/ip
XiYang-DING12 小时前
【WebSocket】文本帧Text Frame
网络·websocket·网络协议
Novproxy_Z12 小时前
代理 IP 黑白名单体系搭建:自动标记风控 IP,减少封号
网络·网络协议·tcp/ip
RacentCLM13 小时前
HTTPS只在登录时有用?关于TLS的误解讲清
数据库·网络协议·https
可爱系程序猿13 小时前
XML 导入失败并指向 msxml6.dll:用最小样本区分解析错误与组件加载
xml·程序人生·电脑
雪雪爱冲浪13 小时前
用 Kimi 做网页抓取:从零开始的科普指南
网络协议
m0_6399069414 小时前
RESTful API 接口设计规范:资源导向、HTTP 语义、统一返回格式、避坑指南
http·restful·设计规范
洋不写bug14 小时前
HTTP(二):万字解析HTTP交互的请求、响应报文格式
网络协议·http·交互·postman·请求格式·响应格式·restful规范
sun00770014 小时前
ip rule路由
网络·网络协议·tcp/ip