zoukankan      html  css  js  c++  java
  • 十八、发送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");
    }
  • 相关阅读:
    Kafka速览
    分布式消息中间件(二)ActiveMQ
    PageUtil 分页
    Tomcat配置
    CryptographyUtil盐加密
    InitComponent的使用
    Shiro权限总结
    【转】Java自学之路——by马士兵
    ExcelUtil
    Java操作Excel之Poi
  • 原文地址:https://www.cnblogs.com/linjiqin/p/2069621.html
Copyright © 2011-2022 走看看