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

来源:互联网 发布:手机电子书制作软件 编辑:程序博客网 时间:2024/05/16 07:25

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

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

先看必须使用到的jar包。

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

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

下面我们再看看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.xmlsoap.org/soap/envelope/\">");  //xml协议格式请根据自己的soap协议构建。不然无法使用        sb.append("<soap:Body>");          sb.append("<ns2:uploadData xmlns:ns2=\"http://message.biz.apexedu.com/\">");  //要调用的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直接调用的结果

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

结束!

本文为头条号作者发布,不代表今日头条立场。

0 0
原创粉丝点击