• WebServiceUtil


    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.rmi.RemoteException;
    
    import javax.xml.rpc.ServiceException;
    
    import org.codehaus.xfire.client.Client;
    import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
     
    public class WebServiceUtil {
    	 /**
    	 * 静态调用Webservice方法
    	 * @param method
    	 * @param parms
    	 * @return
    	 * @throws ServiceException
    	 * @throws RemoteException
    	 * @throws Exception
    	
    	public static String callService(ServiceMethod method, Object[] parms)
    			throws Exception {
    	 
    		 //	throws ServiceException, RemoteException, Exception {
    		String endPoint = StringUtils.removeEnd(
    				method.getMethodUrl(), "?wsdl");
    		String methodName = method.getMethodName();
    		String nameSpace = method.getNamespace();
    		String username = method.getUserName();
    		String password = method.getPassword();
    		String returnType = method.getReturnType();
    		Service service = new Service();
    		Call call = (Call)service.createCall();
    	//	Call call = new Call(endPoint);
    		// 设置远程webService 端点url
    		call.setTargetEndpointAddress(endPoint);
    		call.setTimeout(Integer.valueOf(120000));
    		// 是否有普通的用户身份认证
    		if (StringUtils.isNotEmpty(username)) {
    			call.setUsername(username);
    			call.setPassword(password);
    		}
    		// 是否设置命名空间(nameSpace)
    		if (StringUtils.isEmpty(nameSpace))
    			call.setOperationName(methodName);
    		else {
    			call.setOperationName(new QName(nameSpace, methodName));
    		}
    		// 设置方法需要的参数——参数名,类型,输入参数
    		int count = 0;
    		for (Object o : parms) {
    			call.addParameter("arg" + count++, Constants.XSD_STRING,
    					ParameterMode.IN);
    		}
    		call.setReturnType(Constants.XSD_ANYSIMPLETYPE);
    		// 通过返回值类型判断WebService的调用方式
    		if (StringUtils.isNotEmpty(returnType)) {
    			Object returnXml = call.invoke(parms);
    			return (returnXml == null) ? null : returnXml.toString();
    		}
    		call.invokeOneWay(parms); 
    		return null;
    	}  */
    	
    	/***
    	 * 
    	 *@Description:获取web服务调用客户端
    	 *@Author:ouyangbiao
    	 *@Date:2016-9-29
    	 *@param callurl
    	 *@return
    	 *@throws Exception
    	 *@Modfiy:
    	 */
    	public static Client getWebServiceClient(String callurl) throws Exception {
    		URL url = new URL(callurl);
    		HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    		httpConnection.setReadTimeout(30000);// 设置http连接的读超时,单位是毫秒
    		httpConnection.setConnectTimeout(30000);
    		httpConnection.connect();
    		InputStream is = httpConnection.getInputStream();
    		Client client = new Client(is, null);
    		try {
    			client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf(30000));// 设置发送的超时限制,单位是毫秒;
    			// 解决Xfire/HttpMethod 造成Socket连接池满
    			// 解决使用netstat -ano命令,发现服务器上有大量的CLOSE_WAIT连接,导致之后的WebService服务访问经常失败
    			client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
    			client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE,"true");
    			return client;
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if(is != null) {
    				is.close();
    			}
    		}
    		return null;
    	}
    
    	/***
    	 * 
    	 *@Description: 调用服务
    	 *@Author:ouyangbiao
    	 *@Date:2016-9-29
    	 *@param in
    	 *@param serviceCode
    	 *@param prssurl
    	 *@param opName
    	 *@return
    	 *@throws Exception
    	 *@Modfiy:
    	 */
    	public static String callService(String url, String opName,Object... params)
    			throws Exception {
    		String callurl =  url;
    		if(callurl.indexOf("?wsdl")<0){
    			callurl=callurl+"?wsdl";
    		}
    		Client client = getWebServiceClient(callurl);
    		Object[] result = client.invoke(opName, params);
    		String rtn = result[0] == null ? "" : result[0].toString();
    		return rtn;
    	}
    }
    
    
    
  • 相关阅读:
    Nginx常用命令
    Nginx进程模型
    华为联运游戏或应用审核驳回:使用花币充值时,对支付延时处理不当,导致商品不到账
    使用Data Ability读取系统设置项
    100个HarmonyOS 2.0开发者Beta公测名额,限时认领!
    【有奖活动】HarmonyOS开发者创新大赛颁奖典礼丨见证星星之火燃爆盛夏
    你其实可以更快!用Toolkit拖拽式编码方法帮你快速提升开发效率
    真机调试设备不够?华为AGConnect云调试帮你忙
    华为后台某应用商品展示价格币种随其他应用配置而变化
    HMS Core.Sparkle影音娱乐创新沙龙邀您参加
  • 原文地址:https://www.cnblogs.com/jakaBlog/p/10488352.html
Copyright © 2020-2023  润新知