`
showcup
  • 浏览: 23946 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

HttpURLConnection调用WebService

阅读更多

protected ResponseMessage httpProtocol_http(String businessServiceName,RequestMessage message) throws Exception {

        String url_str = protocol + ":" + "//" + host + ":" + port + uri;

        logger.debug("asip服务地址:" + url_str);

        URL url = new URL(url_str);

        URLConnection connection = url.openConnection();

        /**

         * 构建soap消息

         */

        StringBuffer soapMessage = new StringBuffer();

        soapMessage.append("<soap:Envelope xmlns:soap=\"" + SOAP_NS + "\"  soap:encodingStyle=\"" + ENCODINGSTYLE_NS

                + "\" xmlns:xsd=\"" + XSD_NS + "\" xmlns:xsi=\"" + XSI_NS + "\">");

        soapMessage.append("<soap:Body>");

        soapMessage.append("<ns1:call xmlns:ns1=\"http://service.asip.regaltec.com/\">");

        soapMessage.append("<ns1:in0>").append(businessServiceName).append("</ns1:in0>");

        soapMessage.append("<ns1:in1>").append(StringEscapeUtils.escapeXml(message.toString())).append(

                "</ns1:in1>");

        soapMessage.append("<ns1:in2>").append("").append("</ns1:in2>");

        soapMessage.append("</ns1:call>");

        soapMessage.append("</soap:Body>");

        soapMessage.append("</soap:Envelope>");

        logger.debug("发送到asip的soap消息内容:" + soapMessage);

        byte[] b = soapMessage.toString().getBytes();

        HttpURLConnection con = (HttpURLConnection) connection;

        con.setRequestProperty("Content-Length", String.valueOf(b.length));

        con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

        con.setRequestMethod("POST");

        con.setDoOutput(true);

        con.setDoInput(true);

        OutputStream out = con.getOutputStream();

        out.write(b);

        out.close();

        // 发送完毕

        // 读取响应

        InputStreamReader isr = new InputStreamReader(con.getInputStream());

        BufferedReader in = new BufferedReader(isr);

        String inputLine = null;

        StringBuffer result = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {

            result.append(inputLine);

        }

        in.close();

        logger.debug("接收到asip的soap消息内容:" + result);

        /**

         * 解析soap消息

         */

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setNamespaceAware(true);

        DocumentBuilder builder = factory.newDocumentBuilder();

        Document doc = builder.parse(new InputSource(new StringReader(result.toString())));

        String callResponse = getAsipReturnString(doc);

 

        return ResponseMessage.newInstance(callResponse);

    }

分享到:
评论
1 楼 shixiuteng 2012-08-24  
请问你有没有服务端的代码

相关推荐

Global site tag (gtag.js) - Google Analytics