Java中webService的几种调用方法

xiaoxiao2021-02-28  113

第一种,HttpClient模拟webService:

首先,导入jar包commons-httpclient-3.1.jar,准备模拟请求的基本数据sb

其次,封装请求request对象

再次,发送请求并接受返回结果

最后,得到的结果是XML形式的字符窜,可以用DoM,或sax技术进行报文解析。

形式1:

1

形式2:

public class test{

 public static void main(String[] args) throws Exception {

HttpClient httpclient = new HttpClient();

String url = “http://locahost:9999/ws”;

HttpPost httppost = new HttpPost(url);

                 //定义json对象,里面放请求报文体

JSONObject json = new JSONObject();

               //以下内容根据报文格式决定

                json.put("user","zhangsan" );

                 json.put("pwd","123456" );         json.put("msgtype", "text");//格式

               。。。。。

              JSONObject detailA = new JSONObject(); detailA.put("title", "xxxxxx”);// 非必须,标题 detailA.put("content", "正文");// 必须,正文 json.put("text", detailA);

              。。。。

             StringEntity params = new StringEntity(json.toString(), "UTF-8");      httppost.setEntity(params);     HttpResponse response = httpclient.execute(httppost);//获得响应

            int code=response.getStatusLine().getStatusCode() //获得响应吗,200为请求成功,作为判断使用

           /* 读返回数据 */    String conResult = EntityUtils.toString(response.getEntity());    JSONObject sobj = new JSONObject();   sobj = sobj.fromObject(conResult);   String errmsg = sobj.getString("errmsg");   String errcode = sobj.getString("errcode");

          。。。。

         以下根据业务需求。。。。。

 }

}

第二种,Jax_wbservice标准调用方式:服务器端地址改变,方便改

第三种,通过CXF调用web服务

1、 先通过(*wsimport 生成客户端)

2、把生成的代码拷贝到你的程序中

3、导入CXF的依赖jar包,

4、调用,com.zhj.service包就是通过import生成的文件包,拷贝到项目中。

下面说一说CXF+Spring调用webservice:

Ø 导入 spring cxf 的依赖 jar: Ø wsimport 生成客户端代码,拷贝到本项目 Ø Spring 配置文件中增加配置,将服务客户端纳入工厂 web.xml中配置: 第四种、CXF RestFul风格的web service调用。 优点:是不需要生成客户端代码,只需要知道接口发布的网址即可 缺点:服务器端必须也是用RestFul风格发布的。 详情请查看我本博客中的另一篇文章: Java CXF RestFul风格的web service发布与调用 http://blog.csdn.net/zhangyunfeixyz/article/details/72912244 第五种、axis2远程调用webservice

TestClient.java   package example.client;       import org.apache.axiom.om.OMAbstractFactory;   import org.apache.axiom.om.OMElement;   import org.apache.axiom.om.OMFactory;   import org.apache.axiom.om.OMNamespace;   import org.apache.axis2.addressing.EndpointReference;   import org.apache.axis2.client.Options;   import org.apache.axis2.client.ServiceClient;       public class TestClient {       // targetEPR指定打包的Service(.aar文件)在容器中的物理位置。          private static EndpointReference targetEPR=new EndpointReference             ("http://localhost:8080/axis2/services/HelloWorld");          public static OMElement getSayHelloOMElement(){           //创建request SOAP包。                  OMFactory fac=OMAbstractFactory.getOMFactory();           // OMNamespace指定此SOAP文档名称空间。                  OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");           //创建元素sayHello,并指定其在omNs指代的名称空间中。                  OMElement method=fac.createOMElement("sayHello",omNs);           //指定元素的文本内容。                  method.setText("ZJ");                 return method;           }          public static void main(String[] args){                 try{                         Options options=new Options();                         options.setTo(targetEPR);                         ServiceClient sender=new ServiceClient();                         sender.setOptions(options);                         OMElement sayHello=TestClient.getSayHelloOMElement();               //发出request SOAP,   //同时将得到的远端由sayHello方法返回的信息保存到result。   //通过services.xml能准确找到sayHello方法所在的文件。                         OMElement result=sender.sendReceive(sayHello);                  }                 catch(Exception axisFault){                         axisFault.printStackTrace();                  }           }   }

第六种、xfire的发布与调用,这里不说了,网上一搜一片,照着学习就好了。

转载请注明原文地址: https://www.6miu.com/read-71839.html

最新回复(0)