使用Axis2调用webservice

在项目开发中,我们目前经常要调用webservice,现在来分享一下关于Axis2调用webservice的方法。方法如下:
public String wxLogin(String clientId, String clientPwd, String sessionInfo, String terminalInfo,
			String userName, String passWord, String dataType) {
		String result = null;
		String[] val = new String[]{clientId,clientPwd,sessionInfo,terminalInfo,userName,passWord,dataType};
		try {
			String url = "http://192.168.0.55/项目名/services/接口名?wsdl";//一般是这种格式,根据自己需求修改
			String nameSpace = "http://webservice命名空间" ;//接口的命名空间
			
			RPCServiceClient client = new RPCServiceClient();
			Options options = client.getOptions();
			EndpointReference end = new EndpointReference(url);
			options.setTo(end);
			options.setManageSession(true); 
			options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true); 
			
			options.setAction("http://命名空间/要访问的方法名");
            
            Object[] obj = new Object[]{val};  
              
            QName qname = new QName(nameSpace, "方法名");
            	            
            OMElement omElement = client.invokeBlocking(qname, obj);  
            result = omElement.getFirstElement().getText();
			System.out.println(" result  --- " + result );
			client.cleanupTransport();//释放资源
		} catch (AxisFault e) {
			e.printStackTrace();
			result = e.toString();
		}
		return result;
	}
然后在实际的前端调用webservice的时候出现了访问两次之后就会出现连接超时的问题,原因是因为没有释放资源,因此需要加上上面的:
options.setManageSession(true); 
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true); 
client.cleanupTransport();

特别注意最后这段代码要放在方法的最后面,不然会无效。这样就可以访问成功webservice接口了。

猜你喜欢

转载自blog.csdn.net/star950117/article/details/80883790