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());
    }
}
相关推荐
c++之路7 小时前
Linux网络协议与编程基础:TCP/IP协议族全解析
linux·网络协议·tcp/ip
青槿吖8 小时前
第二篇:告别XML臃肿配置!Spring注解式IOC/DI保姆级教程,从入门到真香
xml·java·开发语言·数据库·后端·sql·spring
半壶清水8 小时前
[软考网规考点笔记]-数据通信基础之差错控制编码技术
网络·笔记·网络协议·tcp/ip
sbjdhjd9 小时前
RHCE | Web 服务器与 Nginx 全栈详解
linux·nginx·http·云原生·oracle·架构·web
c++之路10 小时前
HTTP协议全解析:从原理到Web应用实战
前端·网络协议·http
rell33611 小时前
机顶盒播放udp/rtp马赛克
java·网络·网络协议·udp
lied166363480612 小时前
配置网站HTTP 自动跳转 HTTPS
网络协议·http·https
tang7778912 小时前
哪些行业用动态代理ip?哪些行业用静态代理IP?怎样区分动态ip和静态ip?(互联网人必码·实用长文)
大数据·网络·爬虫·python·网络协议·tcp/ip·智能路由器
Du_chong_huan12 小时前
《网络是怎样连接的》精读版 第四章总述
网络·网络协议·计算机网络
tang7778912 小时前
爬虫代理IP池到底有啥用?
网络·爬虫·python·网络协议·tcp/ip·ip