Axis、Axis2、CXF调用WebService的几种方式
程序员文章站
2022-06-11 08:21:36
...
第一种:
Axis调用webService:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
(注意这里引用的包不是axis2是axis)
public static String ask(String url,String method,Object[] params){
String result = null;
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setProperty(Call.CONNECTION_TIMEOUT_PROPERTY, new Integer(
60 * 60000));
call.setTimeout(new Integer(60 * 60000));
call.setOperationName(method);//WSDL里面描述的接口名称
result = (String)call.invoke(params);
} catch (Exception e) {
log.error("调用webservice接口异常");
e.printStackTrace();
}
return result;
}
第二种:
axis2调用webService
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
(注意这里引用的是axis2的包)
public void testRPC(){
try{
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
// String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
String url = "http://localhost:8089/webserviceServer/TestService?wsdl";
EndpointReference end = new EndpointReference(url);
options.setTo(end);
Object[] obj = new Object[]{"tom"};
Class<?>[] classes = new Class[] { String.class };
QName qname = new QName("http://testClick", "sayHello");
String result = (String) client.invokeBlocking(qname, obj,classes)[0];
System.out.println(result);
}catch(AxisFault e){
e.printStackTrace();
}
}
第三种:
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
(引入的包同第二种一样)
public void testOMElement(){
try{
ServiceClient sc = new ServiceClient();
Options opts = new Options();
String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
EndpointReference end = new EndpointReference(url);
opts.setTo(end);
opts.setAction("sayHello");
sc.setOptions(opts);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://testClick", "");
OMElement method = fac.createOMElement("sayHello",omNs);
OMElement value = fac.createOMElement("name",omNs);
value.setText("admin");
method.addChild(value);
OMElement res = sc.sendReceive(method);
res.getFirstElement().getText();
System.out.println(res.getFirstElement().getText());
}catch(AxisFault e){
e.printStackTrace();
}
}
CXF调用webService:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String cvsUrl = "http://xxxxxx?wsdl";
Client client = dcf.createClient(cvsUrl);
String params = "{\"username\":\""+config.getDataAcc()+"\",\"password\":\""+config.getDataPwd()+"\",\"jobName\":\""+holeTask.getTaskName()+"\"}";
objects = client.invoke("generalAssetResult", params);
参考文档:
http://www.2cto.com/kf/201311/257446.html
Axis调用webService:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
(注意这里引用的包不是axis2是axis)
public static String ask(String url,String method,Object[] params){
String result = null;
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setProperty(Call.CONNECTION_TIMEOUT_PROPERTY, new Integer(
60 * 60000));
call.setTimeout(new Integer(60 * 60000));
call.setOperationName(method);//WSDL里面描述的接口名称
result = (String)call.invoke(params);
} catch (Exception e) {
log.error("调用webservice接口异常");
e.printStackTrace();
}
return result;
}
第二种:
axis2调用webService
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
(注意这里引用的是axis2的包)
public void testRPC(){
try{
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
// String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
String url = "http://localhost:8089/webserviceServer/TestService?wsdl";
EndpointReference end = new EndpointReference(url);
options.setTo(end);
Object[] obj = new Object[]{"tom"};
Class<?>[] classes = new Class[] { String.class };
QName qname = new QName("http://testClick", "sayHello");
String result = (String) client.invokeBlocking(qname, obj,classes)[0];
System.out.println(result);
}catch(AxisFault e){
e.printStackTrace();
}
}
第三种:
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
(引入的包同第二种一样)
public void testOMElement(){
try{
ServiceClient sc = new ServiceClient();
Options opts = new Options();
String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
EndpointReference end = new EndpointReference(url);
opts.setTo(end);
opts.setAction("sayHello");
sc.setOptions(opts);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://testClick", "");
OMElement method = fac.createOMElement("sayHello",omNs);
OMElement value = fac.createOMElement("name",omNs);
value.setText("admin");
method.addChild(value);
OMElement res = sc.sendReceive(method);
res.getFirstElement().getText();
System.out.println(res.getFirstElement().getText());
}catch(AxisFault e){
e.printStackTrace();
}
}
CXF调用webService:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String cvsUrl = "http://xxxxxx?wsdl";
Client client = dcf.createClient(cvsUrl);
String params = "{\"username\":\""+config.getDataAcc()+"\",\"password\":\""+config.getDataPwd()+"\",\"jobName\":\""+holeTask.getTaskName()+"\"}";
objects = client.invoke("generalAssetResult", params);
参考文档:
http://www.2cto.com/kf/201311/257446.html
上一篇: 浅析php与数据库代码开发规范_PHP
下一篇: Illegal character (NULL, unicode 0) encountered: not valid in any content
推荐阅读
-
axis2通过城市名称调用.net写的asmx WebService查询天气实例
-
Axis、Axis2、CXF调用WebService的几种方式
-
Axis、Axis2、CXF调用WebService的几种方式
-
axis2客户端调用免费的webservice服务的实例之三axis2使用RPC方
-
Axis2 WebService的配置、发布、调用
-
axis2客户端调用免费的webservice服务的实例之二纯手动调用免费
-
php中nusoap调用java axis2发布的webservice
-
php中nusoap调用java axis2发布的webservice
-
axis2通过城市名称调用.net写的asmx WebService查询天气实例
-
axis2实现HTTPs双向认证的几种方式