随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

十八、发送xml数据给服务器

一、发送xml数据

复制代码
public static void main(String[] args) throws Exception {
String xml
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><videos><video><title>中国</title></video></videos>";
String path
=  http://localhost:8083/videoweb/video/manage.do?method=getXML ;

byte[] entity = xml.getBytes("UTF-8");
HttpURLConnection conn
= (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(
5000);
conn.setRequestMethod(
"POST");
conn.setDoOutput(
true);
//指定发送的内容类型为xml
conn.setRequestProperty(
"Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty(
"Content-Length", String.valueOf(entity.length));
OutputStream outStream
= conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode() == 200){
System.out.println(
"发送成功");
}
else{
System.out.println(
"发送失败");
}
}
复制代码

             

                 

二、接受xml数据

复制代码
public ActionForward getXML(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
InputStream inStream
= request.getInputStream();
byte[] data = StreamTool.read(inStream);
String xml
= new String(data, "UTF-8");
System.out.println(xml);
return mapping.findForward("result");
}
复制代码
posted on   Ruthless  阅读(2902)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
< 2011年6月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示