1.Axis method
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 service name* @param nameSpace * @param methodName executed by Law name* @param paremateArrs parameter data array* @param qNameArrs variable array* @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);//If this sentence is not written, it will report the error that the server fails to recognize the value of the HTTP header SOAPAction for(int i= 0,len=qNameArrs.length;i call.addParameter(new QName(nameSpace,qNameArrs[ i].toString()), org.apache. axis.encoding.XMLType.XSD< /span>_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 span>();} 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 method
The cxf method is divided into static invocation and dynamic invocation. Static calls need to rely on the service class, the client must provide an interface that is exactly the same as the server, and the package name must also be the same. The dynamic call does not depend on the service class at all, and the server can be easily called as long as the interface name and path are provided.
**Static call**
import org.apache .cxf.endpoint.Client;< /span>import org.apache.cxf.frontend< /span>.ClientProxy;import org.apache span>.cxf.jaxws.JaxWsProxyFactoryBean ;import org.apache.cxf< span class="hljs-preprocessor">.transport.http.HTTPConduit;import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;import cn.com.webxml.MobileCodeWSSoap;//CXF static call//static call Need to rely on the service class, the client must provide an interface that is exactly the same as the server, and the package name must also be consistent 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); span> policy.setReceiveTimeout(120 * 1000); contduit.setClient(policy); String responsemsg = service.getMobileCodeInfo("18502115032", ""); System< span class="hljs-preprocessor">.out.println("Return message:" span> + responsemsg);}}
Interface class:
package cn.com.webxml;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;< span class="hljs-keyword">import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was ge nerated 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/")< span class="hljs-keyword">public interface MobileCodeWSSoap { /** *
* Get information about the province, region and type of mobile phone card where the domestic mobile phone number belongs
* * Input parameters: mobileCode = string (mobile phone number, at least the first 7 digits), userID = string (business user ID) * free user is an empty string; return data: string (mobile phone number: province city Phone card type). *
*
* * @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< /span> String getMobileCodeInfo( @WebParam(name = " mobileCode", targetNamespace = "http://WebXml.com.cn/") String mobileCode, @ WebParam(name = "userID", targetNamespace = "http://WebXml.com.cn/" ) String userID);}
Dynamic call
import org .apache.cxf.endpoint.Client ;import org.apache.cxf< /span>.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;//CXF dynamic call// Dynamic call does not depend on the service class at all, the server only Provide the interface name and path to easily call 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",< span class="hljs-string">""}); System.out.println(res[0] .toString());} catch (Exception e) {e.printStackTrace(); }}}
3.XFire method strong>
import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;/ /CXF dynamic call// Dynamic call does not depend on the service class at all. As long as the server side provides the interface name and path, you can easily call public class Test2 {public static void main(String[] args) {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(< span class="hljs-string">"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 method
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws .endpoint.dynamic.JaxWsDynamicClientFactory span>;//CXF dynamic call // Dynamic call does not depend on the service class at all. As long as the server side provides the interface name and path, it is convenient to call 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",< span class="hljs-string">""}); System.out< span class="hljs-preprocessor">.println(res[0].toString ());} catch (Exception e) {e.printStackTrace(); }}}
5. Use Myeclipse to generate the client in reverse based on the WSDL file< /strong>
The advantage of this method is that there is no need to add any jar files, which can effectively avoid adding jar package conflicts. question.
-
File=》New=》Other
-
Select Web Service Client and click Next button< br>
-
Select the corresponding item and click the Next button
-
Enter the WSDL URL address, click the Nex button, and finally click the Finish button to generate the following java class
-
Call method
li>
package cn.com.webxml;public class Test{ public static void main(String[] args) {//Get a WS Service MobileCodeWS mobileCodeWS = new MobileCodeWS(); //Get specific service type soap: such as post, get , Soap1.1, soap1.2 MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap(); //Call method request String returnString = mobileCodeWSSoap.getMobileCodeInfo("18502115030", null); System.out. println(returnString); }}
Source code download address: WebService calling method source code
1. Axis method
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 service name* @param nameSpace * @param methodName executed method name* @param paremateArrs parameter data array* @param qNameArrs variable array* @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);//This sentence is not The writing will report the error that the server failed to recognize the value of the HTTP header SOAPAction for(int i=0,len=qNameArrs.length;i call.addParameter(new QName(nameSpace,qNameArrs[i].toString()), org.apache.axis .encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode< span class="hljs-preprocessor">.IN ); } call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); call. setTargetEndpointAddress(new URL(endPoint)); returnObj = call.invoke(paremateArrs); }catch(Exception ie){ ie.printStackTrace span>();} System.out.println< /span>(returnObj); return returnObj;} public static void main(String[] args) { String url="http://ws.webxml.com.cn/W ebServices/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 method
The cxf method is divided into static invocation and dynamic invocation. Static calls need to rely on the service class, the client must provide an interface that is exactly the same as the server, and the package name must also be the same. The dynamic call does not depend on the service class at all, and the server can be easily called as long as the interface name and path are provided.
**Static call**
import org.apache .cxf.endpoint.Client;< /span>import org.apache.cxf.frontend< /span>.ClientProxy;import org.apache span>.cxf.jaxws.JaxWsProxyFactoryBean ;import org.apache.cxf< span class="hljs-preprocessor">.transport.http.HTTPConduit;import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;import cn.com.webxml.MobileCodeWSSoap;//CXF static call//static call Need to rely on the service class, the client must provide an interface that is exactly the same as the server, and the package name must also be consistent 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); span> policy.setReceiveTimeout(120 * 1000); contduit.setClient(policy); String responsemsg = service.getMobileCodeInfo("18502115032", ""); System< span class="hljs-preprocessor">.out.println("Return message:" span> + responsemsg);}}
Interface class:
package cn.com.webxml;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;< span class="hljs-keyword">import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was gener ated 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/")< span class="hljs-keyword">public interface MobileCodeWSSoap { /** *
* Get information about the province, region and type of mobile phone card where the domestic mobile phone number belongs
* * Input parameters: mobileCode = string (mobile phone number, at least the first 7 digits), userID = string (business user ID) * free user is an empty string; return data: string (mobile phone number: province city Phone card type). *
*
* * @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调用方式源代码