java post xml

public void  demo10() {
        String xmlData = "<![CDATA["+ "1111111"+"]]>";
        String data="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.regulatoryplat.jsntuah.com\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ser:unRegApp>\n" +
                "         <!--Optional:-->\n" +
                "         \n" +
                "      <arg0>"+xmlData+"</arg0></ser:unRegApp>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        try {
            //第一步:创建服务地址
            URL url = new URL("http://127.0.0.1:8081/webservice/api?wsdl");
            //第二步:打开一个通向服务地址的连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //第三步:设置参数
            //3.1发送方式设置:POST必须大写
            connection.setRequestMethod("POST");
            //3.2设置数据格式:content-type
            connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
            //3.3设置输入输出,因为默认新创建的connection没有读写权限,
            connection.setDoInput(true);
            connection.setDoOutput(true);

            //第四步:组织SOAP数据,发送请求
            String soapXML = data;
            //将信息以流的方式发送出去
            // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面

            OutputStream os = connection.getOutputStream();
            os.write(soapXML.getBytes());
            //第五步:接收服务端响应,打印
            int responseCode = connection.getResponseCode();
            if(200 == responseCode){//表示服务端响应成功
                //获取当前连接请求返回的数据流
                InputStream is = connection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                StringBuilder sb = new StringBuilder();
                String temp = null;
                while(null != (temp = br.readLine())){
                    sb.append(temp);
                }

                is.close();
                isr.close();
                br.close();
                System.out.println(StringEscapeUtils.unescapeXml(sb.toString()));    //转义
                System.out.println(sb.toString());
                cn.hutool.json.JSONObject json = XML.toJSONObject(sb.toString());
                System.out.println(json.getJSONObject("soap:Envelope").getJSONObject("soap:Body").getJSONObject("ns2:unRegAppResponse").getStr("String"));

            } else { //异常信息
                InputStream is = connection.getErrorStream();    //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。
                InputStreamReader isr = new InputStreamReader(is,"utf-8");
                BufferedReader in = new BufferedReader(isr);
                String inputLine;
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream("d:\\result.xml")));// 将结果存放的位置
                while ((inputLine = in.readLine()) != null)
                {
                    System.out.println(inputLine);
                    bw.write(inputLine);
                    bw.newLine();
                    bw.close();
                }
                in.close();
            }

            os.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值