目前比较流行的提供远程服务的技术中,WebService算是比较流行之一。因此,在调用WebService远程服务的客户端代码也是我们经常碰到的。本人把自己在开发中调用WebService远程服务的客户端代码总结下,算是留个笔记,方便以后使用。
1.使用Axis调用
查看wsdl描述文件,wsdl:portType 暴露的是远程接口名称;wsdl:operation 对应的name 为远程接口暴露的方法,一般情况下 wsdl:deFinitions 定义的targetNamespace对应的是接口的包的反向。
import java.net.URL;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.Service;
public class AxisClientSample {
public static void main(String[] args) throws Exception {
String[] recipients = new String[]{}; //收件人
String strSubject = ""; //主题
String strContent = ""; //内容
String[] ccRecipients = new String[]{}; //抄送人
String[] bccRecipients = null; //密送人
String endPoint = " http://service_host:port/appProject/services/XXXXService";
Service server = new Service();
Call call = (Call) server.createCall();
call.setTargetEndpointAddress(new URL(endPoint));
//call.getMessageContext().setUsername("username"); //如果远程方法需要用户名和密码验证时
//call.getMessageContext().setPassword("password");
String resp = (String) call.invoke("exposeMethod",new Object[]{recipients,strSubject,strContent,ccRecipients,bccRecipients});
System.out.println(resp);
}
}
XXXXServiceService service = new XXXXXLocator(); //获取远程服务
XXXXService stub = service.getXXXXService(); //XXXXService是远程服务接口类
stub.exposeMethod(args);
2.XFire 调用
import java.net.MalformedURLException;
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 org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class XFireClientSample {
public static void main(String[] args) {
Service srModel = new ObjectServiceFactory().create(XXXService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
Service srModel = new ObjectServiceFactory().create(XXXService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String endPoint = "
http://service_host:port/app/services/XXXService";
try {
XXXService service = (XXXService) factory.create(srModel,endPoint);
String[] recipients = new String[] {}; // 收件人
String strSubject = ""; // 主题
String strContent = ""; // 内容
String[] ccRecipients = new String[] {}; // 抄送人
String[] bccRecipients = null; // 密送人
System.out.print("提交返回:" + service.getmethod(recipients,bccRecipients));
} catch (MalformedURLException e) {
e.printstacktrace();
}
}
try {
XXXService service = (XXXService) factory.create(srModel,endPoint);
String[] recipients = new String[] {}; // 收件人
String strSubject = ""; // 主题
String strContent = ""; // 内容
String[] ccRecipients = new String[] {}; // 抄送人
String[] bccRecipients = null; // 密送人
System.out.print("提交返回:" + service.getmethod(recipients,bccRecipients));
} catch (MalformedURLException e) {
e.printstacktrace();
}
}
}
3.使用CXF 调用
import org.apache.cxf.jaxws.JaxWsProxyfactorybean;
import test.demo.service.XXXService;
public class CXFClientSample {
public static void main(String[] args) throws Exception{
JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
factory.setAddress(" http://remote_host:port/service/XXXService");
factory.setServiceClass(XXXService.class);
XXXService service = (XXXService) factory.create();
String username="sample";
String response = service.executeMethod(username);
System.out.println(response);
}
public static void main(String[] args) throws Exception{
JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
factory.setAddress(" http://remote_host:port/service/XXXService");
factory.setServiceClass(XXXService.class);
XXXService service = (XXXService) factory.create();
String username="sample";
String response = service.executeMethod(username);
System.out.println(response);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。