java动态调用webservice(.asmx?wsdl)
一 、web格式的接口动态调用代码
这个是之前调用webservise的动态接口,用着一直OK,直到这个词对接.asmx?wsdl的接口就报错了
/*webservice接口调用*/ private Object[] connectWSCore(String ifUrl,String method,Document document){ JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); System.out.println("ifUrl===="+ifUrl); Client client = factory.createClient(ifUrl); // 需要密码的情况需要加上用户名和密码 //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD)); HTTPConduit conduit = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setConnectionTimeout(2000); //连接超时 httpClientPolicy.setAllowChunking(false); //取消块编码 httpClientPolicy.setReceiveTimeout(120000); //响应超时 conduit.setClient(httpClientPolicy); Object[] objects = null; try { String s = document.asXML(); System.out.println("数据:--->>>" + s); objects = client.invoke(method, s); String result = objects.toString(); System.out.println("返回数据:result---->>>" + result); System.out.println("返回数据:---->>>" + objects[0]); } catch (Exception e) { e.printStackTrace(); } return objects; } /** * 消息推送接口 */ public String connectWS(String url,String method,Map<String,String> param) { Document document = DocumentHelper.createDocument(); Element root = document.addElement("request"); param.forEach((key,val)->{ root.addElement(key).addText(val); }); try { Object[] objects = connectWSCore(url,method,document); return objects.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; }
一 、asmx格式的接口动态调用代码
首页说明接口用soapui是可以测试成功的;
调用中用了如下两段代码,期间报错如下两条:
Q1:webserver引用出现”服务器未能识别 HTTP 头 SOAPAction 的值“
Q2:服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。
private Call getCall2(String url, String method){ Call call = null; try { call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(url)); String soapaction = "http://tempuri.org/"; call.setUseSOAPAction(true); soapaction = soapaction+ method; log.info("path:--->>>" + soapaction); call.setSOAPActionURI(soapaction); call.setOperationName(method);// WSDL里面描述的接口名称 // call.setOperationName(new QName(soapaction, method)); call.addParameter("Msg", XMLType.XSD_STRING, ParameterMode.IN); // call.setReturnType(new QName(soapaction, method),String.class); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型 // call.addParameter("Body", XMLType.XSD_STRING, ParameterMode.OUT); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ServiceException e1) { e1.printStackTrace(); } return call; }
/*webservice接口调用*/ private NodeList connectWSCore(String ifUrl, String method, String param){ JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); System.out.println("ifUrl===="+ifUrl); Client client = factory.createClient(ifUrl); // 需要密码的情况需要加上用户名和密码 //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD)); HTTPConduit conduit = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setConnectionTimeout(2000); //连接超时 httpClientPolicy.setAllowChunking(false); //取消块编码 httpClientPolicy.setReceiveTimeout(120000); //响应超时 conduit.setClient(httpClientPolicy); Object[] objects = null; try { // String res = callWebserviceASMX(ifUrl,"Msg",method,param); // log.info("数据res:--->>>" + res); // param = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+param; System.out.println("数据:--->>>" + param); log.info("数据:--->>>" + param); /*if (ifUrl.contains(".asmx")) { log.info("asmx:1--->>>true"); QName qName = getOperateQName(client,method); log.info("asmx:2--->>>true"); objects = client.invoke(qName, param); } else { objects = client.invoke(method, param); }*/ objects = client.invoke(method, param); log.info("objects======:"+objects.length); String result = objects.toString(); System.out.println("返回数据:result---->>>" + result); System.out.println("返回数据:---->>>" + objects[0]); log.info("result======:"+result); org.dom4j.Document document = DocumentHelper.parseText(objects.toString()); log.info("dom4j======:"+document); // org.dom4j.Element root = document.getRootElement(); Document doc = DocTransformUtil.parse(document); log.info("w3c======:"+doc); Element root = doc.getDocumentElement(); return root.getChildNodes(); } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } return null; }
最后使用如下连接中的代码解决,代码直接粘贴他的了
https://blog.csdn.net/pengshengli/article/details/84136309
public static void GetStudent() { try { String url ="http://localhost:9999/WebService.asmx"; String namespace = "http://tempuri.org/"; Service service = new Service(); Call call = (Call) service.createCall(); QName qname = new QName(namespace, "GetdbData"); //设置命名空间和需要调用的方法名 call.setOperationName(qname); call.setTargetEndpointAddress(url); call.setSOAPActionURI(namespace + "GetdbData"); call.addParameter(new QName(namespace,"Name"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// 接口的参数 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 String result = (String) call.invoke(new Object[] {"Tom"}); System.out.println("结果: \n" + result); } catch (Exception e) { System.err.println(e.toString()); } }