目录

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());
    }
}
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
codingandsleeping18 分钟前
OPTIONS 预检请求
前端·网络协议·浏览器
海滩上的那乌克丽丽3 小时前
通过websocket给服务端发送订单催单提醒消息
网络·websocket·网络协议
codingandsleeping3 小时前
DNS 解析规则
网络协议·浏览器·dns
笨手笨脚の3 小时前
详解 Https 和加密、摘要、签名、数字证书
网络协议·https·加密·数字证书·ca·摘要
小冯的编程学习之路5 小时前
【HTTP】:应用层协议HTTP(1)
linux·网络·网络协议·http
言成言成啊5 小时前
手撕TCP内网穿透及配置树莓派
网络·网络协议·tcp/ip·内网穿透
serve the people5 小时前
ip route show 命令详解
网络·网络协议·tcp/ip
昊昊该干饭了6 小时前
数据采集爬虫三要素:User-Agent、随机延迟、代理ip
爬虫·网络协议·tcp/ip·网络爬虫
秋名RG6 小时前
HTTP 1.1 比 HTTP1.0 多了什么?(详尽版)
网络·网络协议·http
上海云盾-高防顾问7 小时前
高防IP可以防御什么类型攻击?解析其核心防护能力与应用场景
网络·网络协议·tcp/ip