使用HttpClient调用CFX WebService

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanqizi/article/details/78558276

首先要得到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;  

        }  

猜你喜欢

转载自blog.csdn.net/zhanqizi/article/details/78558276