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
相关推荐
杨凯凡14 分钟前
Spring Framework 7 HTTP Interface 实战:声明式 RestClient、超时与测试沫璃染墨2 小时前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制CodeYp20193 小时前
第11讲:应用层协议 HTTPwdeok20 小时前
Socket编程UDP-简单的聊天室Thanks_ks1 天前
HTTP 请求方法与 RESTful 接口设计:GET 和 POST 到底怎么用LK_072 天前
【FastAPI】基础篇 01|开始之前:API、HTTP 与 FastAPI 到底是什么阿星_2 天前
pydio登陆不了分析问题Joy T2 天前
Gzip 压缩原理与实践:HTTP 响应压缩、性能收益与工程配置博客zhu虎康4 天前
实现局域网P2P通信:内网发送消息xixingzhe25 天前
AI 对话打字机效果:SSE 和 HTTP 的区别与适用场景