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
相关推荐
我今晚不熬夜10 小时前
Java语言实现Modbus协议通过用串口读取数据(以RTU为例)田里的水稻12 小时前
EP_XML\JSON配置文件和YAMLDLYSB_15 小时前
《从零开始搭建机房轻量化“声光告警”系统:基于 HTTP API 与 TTS 的运维实践》ZhengEnCi15 小时前
N3A-一个端口只能给一个程序使用吗?发光的沙子15 小时前
FPGA----配置根文件系统ip和mac地址Arbori_2621516 小时前
TCP 通信协议xiangji17 小时前
开源完美模块组件化可扩展的Xml解析器Hand.ParseXml一棵树735117 小时前
跨域资源共享cors华清远见成都中心1 天前
物联网通信协议对比:MQTT、CoAP、HTTP到底该怎么选