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
相关推荐
可乐鸡翅yeah_1 分钟前
hls.js 手动自定义 http 请求 loader,修改请求头实战pt10432 小时前
CML网络仿真入门-2:使用CML模拟网络实验环境熊文豪7 小时前
KingbaseES 数据加密全解:从 SSL 到全密态网络小江8 小时前
上网第三十八课:TCP 三次握手与四次挥手——连接到底是怎么“谈“成的AIgorithmGEEK8 小时前
[Linux]HTTP 应用层协议全解(下篇)DP DPharness9 小时前
手机连不上 DSH 时的排错顺序:从 401 到 WebSocket 放行91刘仁德9 小时前
传输层协议深度拆解:端口寻址、UDP 报文与 TCP 可靠性机制全解析傲世仙尊9 小时前
TCP报头全解-序号确认应答与可靠性是一个准数AIgorithmGEEK9 小时前
[Linux]HTTP 应用层协议全解(中篇)