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());
    }
}
相关推荐
apihz2 小时前
批量获取3位未注册短域名免费API接口每日更新
android·服务器·网络·网络协议·tcp/ip
北京耐用通信2 小时前
传感器“断联”拖垮产线?耐达讯自动化网关让Ethernet IP转CCLink秒通!
人工智能·科技·物联网·网络协议·自动化
Maryfang132918915513 小时前
RTL8306MB的国产P2P替代方案用JL5106C的可行性分析
网络·网络协议·p2p
编程修仙4 小时前
第五章 Spring XML配置原理
xml·python·spring
白菜__4 小时前
去哪儿小程序逆向分析(酒店)
前端·javascript·爬虫·网络协议·小程序·node.js
2201_757830874 小时前
UDP协议
网络·网络协议·udp
2501_938810115 小时前
比特浏览器适用的IP类型
网络·网络协议·tcp/ip
超级大只老咪5 小时前
常用 HTTP 状态码汇总
网络·网络协议·http
Zabbix_China5 小时前
零配置方案:Zabbix + HTTP 代理实现内网穿透工具的自动化管理
http·自动化·zabbix
苏打水com5 小时前
浏览器与HTTP核心考点全解析(字节高频)
前端·http