Android之通过HTTP协议向服务器发送XML数据

       可以通过装载或读取一个XML文件,得到其数据,然后把得到的数据当成实体,通过HTTP协议用输出流发送给服务器,在服务器端通过获取输入流获取相关数据,这样就是实现了向服务器发送XML数据。如下:
 
客户端:

 public void sendXmlTest() throws Exception{
//通过类装载器装载XML资源
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("test.xml");
byte[] xml=StreamTool.read(inputStream);

String path="http://172.22.35.112:8080/videonews/GetXmlInfo";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoOutput(true);

//设置HTTP请求的头字段
    conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //内容类型
    conn.setRequestProperty("Content-Length", String.valueOf(xml.length));  //实体内容的长度

    conn.getOutputStream().write(xml);  //通过输出流把数据写到服务器
if(conn.getResponseCode()==200){
System.out.println("发送成功!");
}else{
System.out.println("发送失败!");
}
}

 
 
服务器端:
  1. byte[] xml=StreamTool.read(request.getInputStream());  //获得输出流
  2. System.out.println(new String(xml,"UTF-8"));

转载于:https://www.cnblogs.com/allenzheng/archive/2013/05/06/3062610.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值