webService调用工具类

 1 import org.apache.cxf.endpoint.Client;
 2 
 3 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 4 import org.apache.cxf.transport.http.HTTPConduit;
 5 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
 6 
 7 import com.alibaba.fastjson.JSONObject;
 8 
 9 /**
10   * @Description: webservice客户端调用工具类
11   * @CreateDate: 2019/6/18 14:13
12   * @Version: 1.0
13   */
14 public class WSClientUtil {
15   /*public static String clientInvokeWebServiceAxis(String wsdlURL, String nameSpace, String operationName,
16     * webservice客户端调用方法
17     * @param wsdlURL 服务提供地址
18     * @param nameSpace 命名空间
19     * @param operationName 方法名
20     * @param paremateNames 参数名
21     * @param paremateValues 参数值
22     * @return: java.lang.String 返回json字符串
23     * @create: 2019/6/27 10:39
24     * @version: 1.0
25     String[] paremateNames, Object[] paremateValues) throws RemoteException, ServiceException,
26     MalformedURLException {
27     Service service = new Service();
28     Call call = (Call)service.createCall();
29     call.setOperationStyle(Style.WRAPPED);
30     call.setOperationUse(Use.LITERAL);
31     call.setOperationName(new QName(nameSpace, operationName));
32     call.setUseSOAPAction(true);
33     call.setSOAPActionURI(nameSpace + "/" + operationName);
34     call.setEncodingStyle("UTF-8");
35     for (int i = 0; i < paremateNames.length; i++) {
36       call.addParameter(new QName(nameSpace, paremateNames[i]), XMLType.XSD_STRING, ParameterMode.IN);
37     }
38     call.setReturnType(XMLType.XSD_STRING);
39     call.setTargetEndpointAddress(new URL(wsdlURL));
40     Object returnObj = call.invoke(paremateValues);
41     String jsonStr = JSONObject.toJSONString(returnObj);
42     return jsonStr;
43   }*/
44   /**
45     * webservice客户端调用方法
46     * @param wsdlURL 服务提供地址
47     * @param operationName 方法名
48     * @param params 参数值
49     * @return: java.lang.String
50     * @create: 2019/6/27 13:15
51     * @version: 1.0
52     */
53   public static String clientInvokeWebService(String wsdlURL, String operationName, Object... params)  throws Exception {
54     JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
55     Client client = clientFactory.createClient(wsdlURL);
56     HTTPConduit conduit = (HTTPConduit)client.getConduit();
57     HTTPClientPolicy policy = new HTTPClientPolicy();
58     policy.setConnectionTimeout(100000);//设置超时时间
59     policy.setReceiveTimeout(100000);//设置超时时间
60     conduit.setClient(policy);
61     Object[] objects = client.invoke(operationName, params);
62     return JSONObject.toJSONString(objects[0].toString());
63   }
64 }

 

posted @ 2019-07-17 10:24  暖然  阅读(2739)  评论(1编辑  收藏  举报