您的位置:首页 > 编程语言 > Java开发

客户端调用WebService方式总结

2017-10-19 16:11 609 查看

1.Axis方式

import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test2 {
/**
*
* @param serviceUrl 服务名
* @param nameSpace
* @param methodName 执行的方法名
* @param paremateArrs 参数数据数组
* @param qNameArrs 变量数组
* @return
*/
public static Object CallSoapService(String serviceUrl ,String nameSpace,String methodName, Object[] paremateArrs,Object[]  qNameArrs){
String endPoint = serviceUrl;
String actionUrl=nameSpace+methodName;
Object returnObj = null;
try{
Service service = new Service();
Call call = null;
call = (Call)service.createCall();
QName qName = new QName(nameSpace,methodName);
call.setOperationName(qName);
call.setSOAPActionURI(actionUrl);//这一句不写会报服务器未能识别 HTTP 头 SOAPAction 的值的错误
for(int i=0,len=qNameArrs.length;i<len;i++){//设置参数
call.addParameter(new QName(nameSpace,qNameArrs[i].toString()), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setTargetEndpointAddress(new URL(endPoint));
returnObj = call.invoke(paremateArrs);
}catch(Exception ie){
ie.printStackTrace();
}
System.out.println(returnObj);
return returnObj;
}
public static void main(String[] args) {
String url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
String method="getMobileCodeInfo";
String parameter="18502115030";
String namespace="http://WebXml.com.cn/";
Test2.CallSoapService(url,namespace,method,new Object[]{parameter,""},new Object[]{"mobileCode","userID"});

}

}


2.Cxf方式

cxf方式分为静态调用和动态调用。静态调用需要依赖service类,客户端必须提供一个与服务端完全一致的接口,包名也要一致 。动态调用完全不依赖service类,服务器端只要提供接口名和路径就可以方便的调用。
**静态调用**


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import cn.com.webxml.MobileCodeWSSoap;

//CXF静态调用
//静态调用需要依赖service类,客户端必须提供一个与服务端完全一致的接口,包名也要一致
public class Test {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(MobileCodeWSSoap.class);
factory.setAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
MobileCodeWSSoap service = (MobileCodeWSSoap) factory.create();
Client proxy = ClientProxy.getClient(service);
HTTPConduit contduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(20 * 1000);
policy.setReceiveTimeout(120 * 1000);
contduit.setClient(policy);
String responsemsg = service.getMobileCodeInfo("18502115032", "");
System.out.println("返回报文:" + responsemsg);
}
}


接口类:

package cn.com.webxml;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
* This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-
* Generated source version: 2.0
*
*/
@WebService(name = "MobileCodeWSSoap", targetNamespace = "http://WebXml.com.cn/")
public interface MobileCodeWSSoap {

/**
* <br />
* <h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3>
* <p>
* 输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID)
* 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。
* </p>
* <br />
*
* @param userID
* @param mobileCode
* @return returns java.lang.String
*/
@WebMethod(action = "http://WebXml.com.cn/getMobileCodeInfo")
@WebResult(name = "getMobileCodeInfoResult", targetNamespace = "http://WebXml.com.cn/")
@RequestWrapper(localName = "getMobileCodeInfo", targetNamespace = "http://WebXml.com.cn/", className = "cn.com.webxml.GetMobileCodeInfo")
@ResponseWrapper(localName = "getMobileCodeInfoResponse", targetNamespace = "http://WebXml.com.cn/", className = "cn.com.webxml.GetMobileCodeInfoResponse")
public String getMobileCodeInfo(
@WebParam(name = "mobileCode", targetNamespace = "http://WebXml.com.cn/") String mobileCode,
@WebParam(name = "userID", targetNamespace = "http://WebXml.com.cn/") String userID);

}


动态调用

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

//CXF动态调用
// 动态调用完全不依赖service类,服务器端只要提供接口名和路径就可以方便的调用
public class Test2 {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL");
try {
Object[] res = client.invoke("getMobileCodeInfo", new Object[]{"18502115030",""});
System.out.println(res[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}


3.XFire方式

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

//CXF动态调用
// 动态调用完全不依赖service类,服务器端只要提供接口名和路径就可以方便的调用
public class Test2 {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL");
try {
Object[] res = client.invoke("getMobileCodeInfo", new Object[]{"18502115030",""});
System.out.println(res[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}


4.HttpClient方式

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

//CXF动态调用
// 动态调用完全不依赖service类,服务器端只要提供接口名和路径就可以方便的调用
public class Test2 {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL");
try {
Object[] res = client.invoke("getMobileCodeInfo", new Object[]{"18502115030",""});
System.out.println(res[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}


5.使用Myeclipse根据WSDL文件反向生成客户端

这种方式的好处是不用添加任何jar文件,可以有效的避免添加jar包冲突问题。

File=》New=》Other



选择Web Service Client点击Next按钮



选择相应的项目,点击Next按钮



输入WSDL URL地址,点击Nex按钮,最后在点击Finish按钮,生成如下java类



调用方法

package cn.com.webxml;

public class Test{
public static void main(String[] args) {
//获取一个WS服务
MobileCodeWS mobileCodeWS = new MobileCodeWS();
//获取具体的服务类型soap:如post、get、soap1.1、soap1.2
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
//调用方法请求
String returnString = mobileCodeWSSoap.getMobileCodeInfo("18502115030", null);
System.out.println(returnString);
}

}


源码下载地址:WebService调用方式源代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息