分享

Java 使用httpclient Post与cxf 发布的Webservice通信

 观审美2 2017-04-19

使用cxf发布的webservice不知道什么情况总会有时管用有时不管用,对于项目来说这肯定不行。又不想改动webservice因为代码太多。人懒!

于是便使用httpclient与webservice通过肥皂协议通信。

先看必须使用到的jar包。

 

 看看webservice发布的接口,这边我放了两个发布的接口,xml很清晰的写出了两个方法的信息,我们要用到的就是他提供的这些信息。

 

下面我们再看看webService的java写法。这边由于我没注解参数名称所以上面显示的是arg0 . 1 2-  之类的。参数跟返回参数的类型我们都拿到了那下面开始代码了。

 

 

 

soap的话我们使用get可以直接请求,像浏览器一样地址栏输入就ok了。这边主要写下post方法。

首先构建xml

复制代码
/** 
     * 构建请求XML 
     * @return 
     * @throws Exception 
     * @throws IOException 
     */  
    private static String getRequestXml() throws IOException, Exception{  
        String p="aaaaa";
        StringBuilder sb = new StringBuilder();  
        sb.append("<soap:Envelope xmlns:soap=\"http://schemas./soap/envelope/\">");  //xml协议格式请根据自己的soap协议构建。不然无法使用
        sb.append("<soap:Body>");  
        sb.append("<ns2:uploadData xmlns:ns2=\"http://message.biz./\">");  //要调用的webservice服务的方法名称
        sb.append("<arg0>"+p+"</arg0>"); //上面获取到的参数
        sb.append("</ns2:uploadData>");  
        sb.append("</soap:Body>");  
        sb.append("</soap:Envelope>");  
        return sb.toString();  
    }  
  
复制代码
复制代码
  public static void main(String[] args) {
          DefaultHttpClient httpClient = null;  
          try {  
              httpClient = new DefaultHttpClient();  
              HttpPost httppost = new HttpPost(""); //webservice服务地址  
              String soapRequestData = getRequestXml(); //soap协议的格式,定义了方法和参数  
              HttpEntity re = new StringEntity(soapRequestData,HTTP.UTF_8);  
              httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");    
              httppost.setEntity(re);   
              HttpResponse response = httpClient.execute(httppost); //调用接口  
              System.out.println(response.getStatusLine().getStatusCode());  
              if(response.getStatusLine().getStatusCode() == 200) {  //调用状态
                String xmlString = EntityUtils.toString(response.getEntity());  
                String jsonString = parseXMLSTRING(xmlString);  //解析接口返回的值
                String res=de(jsonString);
                  String[] rets = res.split("&");
                System.out.println("成功列表:"+rets[0]);
                System.out.println("失败列表:"+rets[1]);
                if (res.indexOf("error") != 0) {
                    // 上传数据成功
                } else {
                    // 上传数据失败
                    System.out.println(res.substring(6));
                }
              }
          } catch (Exception e) {  
              e.printStackTrace();  
          } finally {  
              httpClient.getConnectionManager().shutdown(); //关闭连接  
          }  
    }
复制代码

 

复制代码
 //解析xml文件,获取其中的返回值  
    public  static String parseXMLSTRING(String xmlString){  
        String returnJson = "";  
         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)  returnJson = node.getFirstChild().getNodeValue();  
             System.out.println("获取的返回参数为:" + returnJson);  
         } catch (Exception e) {     
             e.printStackTrace();     
         }     
        return returnJson;  
    }  
复制代码

下面看post调用结果与cxf直接调用的结果

结束!

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多