基于Java的WebService的客户端开发

1.JAX-WS

          选择“发布”按钮的左边第二个——》选择New Web Service Client——》选择Project与Framework——》在WSDL URL中输入WSDL地址,选择Java source folder与  Java package——》下一步——》会生成很多客户端文件

          再新建一个测试类,写上main方法,在main方法中写上

          StorageYKTService service= new StorageYKTService();

          StorageYKTServiceImplDelegate delegate=service.getStorageYKTService();

          String result=delegate.getDoorStatus("200001", "1", "001");

          System.out.println(result);

 

2.axis

         package com.talkweb.ecard.storage.action;

         import javax.xml.namespace.QName;

         import org.apache.axis.client.Call;
         import org.apache.axis.client.Service;

         public class StorageYKTClient {

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

String url="http://116.55.248.117:8090/HelloWorldService/services/HW";
  //String nameSpaceUri ="http://server";
  String nameSpaceUri ="";
  Service service = new Service();
  Call call = null;
  call = (Call)service.createCall();

  call.setOperationName(new QName(nameSpaceUri,"syncRecord"));
  //call.setOperationName("syncRecord");
  call.setTargetEndpointAddress(new java.net.URL(url));
  Object[] params = new Object[6];
        params[0] = "234";
        params[1] = "12";
        params[2] = "23";

        params[3] = "234";
        params[4] = "12";
        params[5] = "23";

        String ret = (String)call.invoke(params);
  System.out.println("-------------ret:"+ret+"-----------"); 
  
 }
 

        }

也可以这么写

package com.talkweb.ecard.storage.action;

import java.util.Calendar;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class StorageYKTClient {

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

try {
   String endpoint = "http://116.55.248.117:8090/HelloWorldService/services/HW";

    Service  service = new Service();

    Call     call    = (Call) service.createCall();

    call.setTargetEndpointAddress( new java.net.URL(endpoint));

    long timeStart, timeEnd;

    String mobile="18777171717";
    String name="aaa";
    String openDate="20120303122201";
    String doorId="1";
    String dir="1";
    String key="1";
    int a = 9;

    call.setOperationName("syncRecord");

   call.addParameter("mobile",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
   call.addParameter("name",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
   call.addParameter("openDate",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
   call.addParameter("doorId",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
   call.addParameter("dir",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
   call.addParameter("key",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);

    call.setReturnType( XMLType.XSD_STRING);

    timeStart = Calendar.getInstance().getTimeInMillis();

    for(int i=0;i<5;i++)  {

             String ret = (String)call.invoke( new Object[] {mobile,name,openDate,doorId,dir,key} );

             a++;

             System.out.println("Got result : " + ret);

             }

             timeEnd = Calendar.getInstance().getTimeInMillis();

             System.out.println(Long.toString( (timeEnd - timeStart)));

             System.out.println(Float.toString(1000 * ( (float) 1) /(float) (timeEnd - timeStart)));
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 

}

}

 

还可以这么写

package com.talkweb.ecard.storage.action;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class StorageYKTClient {

 public static void main(String[] args) throws Exception {
  String endpoint = "http://116.55.248.117:8090/HelloWorldService/services/HW"; 
  Service service = new Service(); 
  Call call = (Call) service.createCall(); 
  call.setTargetEndpointAddress(new java.net.URL(endpoint)); 
  call.setOperationName("syncRecord");
  
   String mobile="18777171717";
   String name="aaa";
   String openDate="20120303122201";
   String doorId="1";
   String dir="1";
   String key="1";
   String result="11";
  
  result =(String)call.invoke(new Object[]{mobile, name, openDate, doorId, dir, key});
  System.out.println(result);
}

}

3.xfire

     1.在自己的项目中新建一个接口,接口名称随便写,方法名、方法参数、返回值类型必须与服务器的一致

          接口:

           package com.talkweb.ecard.storage.service;

public interface SyncRecord {
 public String syncRecord(String mobile,String name,String openDate,String doorId,String dir,String key);
}

 

客户端:

package com.talkweb.ecard.storage.action;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.talkweb.ecard.storage.service.SyncRecord;

public class StorageYKTClient {

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

String result="";
  String serviceUrl = "http://116.55.248.117:8090/HelloWorldService/services/HW";
  Service serviceModel = new ObjectServiceFactory().create(SyncRecord.class);
  SyncRecord syncrecord=null;
  XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
  
  try {
   syncrecord = (SyncRecord)factory.create(serviceModel, serviceUrl);
  } catch (Exception ex) {
   //ex.printStackTrace();
  }
  result=syncrecord.syncRecord("18711090909", "aaa", "20120305185445", "1", "1","1");
  System.out.println("----------------------------------------------------------------------------");
  System.out.println(result);
  System.out.println("----------------------------------------------------------------------------");

}
}

         也可以不用写接口

 package com.talkweb.ecard.storage.action;
import java.net.URL;

import org.codehaus.xfire.client.Client;        

public class StorageYKTClient {

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

 Client client = new Client(new URL(
  "http://116.55.248.117:8090/HelloWorldService/services/HW?wsdl"));
  Object url[] = client.invoke("syncMember",new Object[]{"1","13312121212","xiao","2","2"});
  
  System.out.println(url[0].toString());
  

      }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值