加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

发送xml数据给服务器

发布时间:2020-12-16 09:19:18 所属栏目:百科 来源:网络整理
导读:http://www.cnblogs.com/linjiqin/archive/2011/06/02/2069621.html 一、发送xml数据 public static void main(String[] args) throws Exception { String xml = "?xml version="1.0" encoding="UTF-8"?videosvideotitle中国/title/video/videos"; Strin

http://www.cnblogs.com/linjiqin/archive/2011/06/02/2069621.html

一、发送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");
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读