使用HttpClient调用CFX WebService

首先要得到webService请求方法的报文也就是soap

下载soapUI模拟请求,soapUi会自动根据wsdl地址生成我们所需要的xml

 也就是这种

拿到这个文件后就是写代码了

 

用浏览器打开wsdl地址拿到

 

    public static void main(String[] args) {

        HttpClient client = new HttpClient(); 

        /*soap:address location*/

        PostMethod method = new PostMethod("");

        client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间

        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");/*设置字符编码*/

        String soapRequestData = getRequestXml();//soap协议的格式,定义了方法和参数  

        try {

            /*设置连接格式请求的xml*/

            RequestEntity re = new StringRequestEntity(soapRequestData, "application/soap+xml", "utf-8");

            method.setRequestEntity(re);

            /*请求连接返回的状态 200请求成功*/

            int state = client.executeMethod(method);

            System.out.println(state);

            /*拿到返回的数据此时返回的是一个soap的xml文件*/

            String data = method.getResponseBodyAsString();

            String js = parseXML(data);

            System.out.println(js);

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        } catch (HttpException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

/*soapui生成的报文*/

private static String getRequestXml(){  

            StringBuilder sb = new StringBuilder();  

            sb.append("<?xml version=\"1.0\"?>");  

            sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ");  

            sb.append(" xmlns:mon=\"http://monitor.ws.platform.isoftstone.com/\">");  

            sb.append("<soapenv:Header/>");  

            sb.append("<soapenv:Body>");  

            /*方法名*/

            sb.append("<mon:getServiceStatus>");

            /*参数1*/

            sb.append("<arg0>weixin</arg0>");

            /*参数2*/

            sb.append("<arg1>weixinpassw0rd</arg1>");

            sb.append("</mon:getServiceStatus>"); 

            sb.append("</soapenv:Body>");  

            sb.append("</soapenv:Envelope>");  

            return sb.toString();  

        }

 

/*解析报文*/

 public  static String parseXML(String xmlString){  

            String result = "";  

             try {     

                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     

                 DocumentBuilder builder = factory.newDocumentBuilder();     

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

                 Element root = doc.getDocumentElement();//根节点   

                 Node node = root.getFirstChild(); 

                 while(!node.getNodeName().equals("return")) {  

                     node = node.getFirstChild();  

                 }  

                 if(node.getFirstChild() != null){

                     result = node.getFirstChild().getNodeValue();  

                 }

                 System.out.println("获取的返回参数为:" + result);  

             } catch (Exception e) {     

                 e.printStackTrace();     

             }     

            return result;  

        }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值