zoukankan      html  css  js  c++  java
  • 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"));
  • 相关阅读:
    matlab2016b和c# .net4.0混合编程
    有限元入门
    math.net 拟合
    excel 错误提示以及其他基础知识
    excel的小bug
    Servlet体系及方法
    Servlet学习笔记
    HTTP协议
    Tomcat
    反射
  • 原文地址:https://www.cnblogs.com/allenzheng/p/3062610.html
Copyright © 2011-2022 走看看