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());
}
}
Http请求Body中携带raw XML形式
demodashi6662024-08-29 19:18
相关推荐
Mikowoo0075 小时前
批量汇总XML格式的发票信息寒晓星5 小时前
【网络编程】UDP编程逑之6 小时前
HTTP、HTTPS2YYYing.7 小时前
【C++大型项目之高性能服务器框架 (九) 】协议抽象与Http服务器模块_waylau8 小时前
Spring Framework HTTP服务客户端详解Android-Flutter8 小时前
Android的http和https知识点爱刷碗的苏泓舒8 小时前
网络通信入门之 NTRIP、HTTP、TCP 与 IP 的关系:协议分层、连接过程以及实时数据流Lhappy嘻嘻8 小时前
网络(四)|全网最细网络层原理:IP 协议、IP 地址分类、子网划分、路由转发、NAT 穿透详解逃逸线LOF17 小时前
xml文件如何加载properties文件(spring容器加载properties文件)我星期八休息18 小时前
网络编程—应用层HTTP协议