打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

使用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.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直接调用的结果

结束!

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
通过HttpClient请求webService
使用HttpClient通过POST方式发送XML
android与php后台服务的数据交换例子webservice1.php
使用CXF发布和调用webservice之HelloWorld入门
WebService介绍
WebService CXF学习(进阶篇3):对象传递
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服