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());
    }
}
相关推荐
爱学习的程序媛13 小时前
ICMP协议详解
网络·网络协议·网络安全·通信协议·icmp·网络运维
隐擎fox13 小时前
解构无感人机验证底层机制:行为生物轨迹采样、环境评分模型与自动化对抗实战
自动化测试·python·网络协议·tcp/ip·网络爬虫
小林ixn16 小时前
跨域、SSE 与 WebSocket:从一次「请求被拦截」说起
websocket·http
yuzhiboyouye19 小时前
my-batisPlus什么时候用xml什么时候用mapper自带的方法,这两种写法专业一点叫什么,什么场景分别使用
xml
captain37620 小时前
网络原理(4)-TCP ▲▲▲
java·服务器·网络·网络协议·tcp/ip
代码中介商20 小时前
C++ 预约系统实战(二):TCP + JSON 自定义协议设计
c++·网络协议
小此方20 小时前
Linux网络(十一):HTTP重定向与请求方法详解:从301/302状态码到GET/POST,再认识Fiddler抓包
linux·网络·http
一个心烑1 天前
配置项目git的 SSL 证书
git·网络协议·ssl
发光小北1 天前
西门子 PLC 串口转网口模块现场应用有哪些问题?
网络协议
楚Y6同学1 天前
QT上位机开发-UDP通信-模拟下位机数据联调的方法
网络协议·udp·联调方法