• 注册
当前位置:1313e > java >正文

基于Java的WebService的客户端开发


分类: WebService 848人阅读 评论(0) 收藏 举报
webservicejavastringexceptionserviceobject

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());

}

}

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录