以下主要是介绍了CxfWebService、AxisWebService、XFireWebService、HttpClientWebService的客户端调用方法
一、AxisWebService调用
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/";
Object ojb = Test2.CallSoapService(url,namespace,method,new Object[]{parameter,""},new Object[]{"mobileCode","userID"});
}
}
二、CxfWebService调用
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("18502115030", "");
System.out.println("返回报文:" + responsemsg);
}
}
//-----------------------------------------------------------------------//
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();
}
}
}
三、XFireWebService
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class Test {
private static Client client;
public static void main(String[] args) {
Object[] results = null;
try {
client = new Client(
new URL(
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl")); // 服务地址
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
results = client.invoke("getMobileCodeInfo", new Object[] {
"18502115030", "" });// 方法名和参数,如果没有参数就传一个空对象数组
} catch (Exception e) {
e.printStackTrace();
}
System.out.println((String) results[0]);
}
}
四、利用eclipse或myEclipse工具生成,具体步骤(https://blog.csdn.net/hfhwfw/article/details/5966150)