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
相关推荐
爱学习的程序媛13 小时前
ICMP协议详解隐擎fox13 小时前
解构无感人机验证底层机制:行为生物轨迹采样、环境评分模型与自动化对抗实战小林ixn16 小时前
跨域、SSE 与 WebSocket:从一次「请求被拦截」说起yuzhiboyouye19 小时前
my-batisPlus什么时候用xml什么时候用mapper自带的方法,这两种写法专业一点叫什么,什么场景分别使用captain37620 小时前
网络原理(4)-TCP ▲▲▲代码中介商20 小时前
C++ 预约系统实战(二):TCP + JSON 自定义协议设计小此方20 小时前
Linux网络(十一):HTTP重定向与请求方法详解:从301/302状态码到GET/POST,再认识Fiddler抓包一个心烑1 天前
配置项目git的 SSL 证书发光小北1 天前
西门子 PLC 串口转网口模块现场应用有哪些问题?楚Y6同学1 天前
QT上位机开发-UDP通信-模拟下位机数据联调的方法