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
相关推荐
进击的荆棘8 小时前
Linux网络——应用层协议HTTP步峰代理-盾山10 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析王亚博_Abel1 天前
域名是怎么变成 IP 的?手拆 DNS 解析四个实验实测用户84913717547161 天前
两版 SNMP 报文只差 1 个字节,我盯了半天才看懂问题出在哪AndreaYYmm1 天前
SIGUSR2 触发热重载:一个正在抓包的进程如何原子地换掉规则集Thanks_ks1 天前
RESTful API 到底是什么:从 URL 设计讲到请求方法的选择用户84913717547161 天前
为什么 SNMP 叫"简单"?真相是:设备少干活,复杂全甩给了你白小飞不会飞1 天前
【AUTOSAR】CP SOME/IP-TP神一样的老师1 天前
用 FCU1501 双网口打通两个不同网段:IP 转发 + NAT 实战keepquietl2 天前
UDS on CAN 入门(6):诊断响应要等多久?读懂 P2、P2* 和 S3