webservice客户端开发

一、根据约定条件生成客户端

1.需要axis.jar包

2.调用

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public static void main(String[] args)(String param1, Date param2, double param3) {
  String url = "http:IP:PORT/url" ;
  try {
    Service service = new Service();
       Call call = (Call) service.createCall();
       //调用的url地址
       call.setTargetEndpointAddress(url);
       //调用的方法名
       call.setOperationName( "doService" );
       //参数,有几个写几个,类型对应
       call.addParameter( "parameter1" ,org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN ) ;
       call.addParameter( "parameter2" ,org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN ) ;
       call.addParameter( "parameter3" ,org.apache.axis.encoding.XMLType.XSD_DOUBLE,javax.xml.rpc.ParameterMode.IN ) ;
       //返回值类型
    call.setReturnType(XMLType.XSD_STRING );
    call.setUseSOAPAction(
true );
    //对应的参数 String result
= (String) call.invoke( new Object[]{param1,param2,param3}) ;   } catch (javax.xml.rpc.ServiceException e) {     e.printStackTrace();   } catch (RemoteException e) {     e.printStackTrace();   } }

 

二、根据wsdl文件生成客户端

1.新建webservice客户端
2.选择wsdl文件、client level、运行时环境,环境用tomcat
3.选择生成的目录
4.启动tomcat
5.确定生成的方法
6.生成代码
7.调用
public static void main(String[] args) {
     //访问webservice的地址
    String url = "http://IP:PORT/url" ;
     try {
        XXXBindingStub binding;
        binding = (XXXBindingStub)new XXXLocator().getXXXHttpSoap11Endpoint(new java.net.URL(url));
        //参数
        String result = binding.send( "param", "param", "param");
        //结果
          System. out.println(result);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ServiceException e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}       

 

posted @ 2016-01-27 17:03  Ruther  阅读(574)  评论(0编辑  收藏  举报