java调用webservice用是axis还是xfire的,两种调用方法不同
淘二哥女装专业导购
http://www.tao2ge.com
我要用JAVA调用一个WEBservice
WEBservice的地址为:http://www.xxxxx.com.cn/webservice/online.asmx
方法名:checkMember
此方法将返回void
怎么样来实现呢?
是那种方法的axis还是xfire的,两种调用方法不同。
axis的
package test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CallService {
public static void main(String[] args) {
try {
Log log = LogFactory.getLog(CallService.class);
String endpoint ="http://localhost:8090/testAxisl/services/UserService";
Service service =new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
// call.setOperationName(new QName("UserService", "save"));//设置操作的名称。
//由于需要认证,故需要设置调用的用户名和密码。
call.getMessageContext().setUsername("sophia");
call.getMessageContext().setPassword("sophia");
//调用的方法
call.setOperationName("getUserByName" );
// //方法中的参数
// call.invoke(new Object[] {"1011","aaaa","aaaa","test"});
call.invoke(new Object[] {"aaaa"});
// call.setOperationName("createUserByUserInfo" );
} catch (ServiceException e) {
e.printStackTrace();
System.out.println("Service 获取 Call对象失败!");
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("new java.net.URL(url)错误!");
} catch (RemoteException e) {
e.printStackTrace();
System.out.println("远程错误!");
}
}
}
xfire的
package test;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import webservice.ClientAuthHandler;
import webservice.HelloWorld;
public class HelloWorldClient {
public static void main(String[] args) {
try{
Service serviceModel = new ObjectServiceFactory().create(HelloWorld.class);
HelloWorld service = (HelloWorld) new XFireProxyFactory().create(serviceModel,
"http://localhost:8090/testWebService/HelloWorldService.ws?");
XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
Client client = proxy.getClient();
//不加用户密码验证会报错,并且终止远程调用
client.addOutHandler(new ClientAuthHandler("system", "manager"));
System.out.println(service.getMes("aaa"));
System.out.println(service.sayHelloWorld("402881861961127c011961c9be590036"));
// System.out.println(service.sayHelloWorld("1001"));
// System.out.println(service.getDemoString("abc"));
}catch(MalformedURLException e){
System.out.println("错误!");
e.printStackTrace();
}
}
}