WebSerivce获取方式和踩坑

最近在做项目的时候,需要用到 webservice请求链接, 碰到了一系列的坑,记录一下这些坑吧。

1.wsdl文件编译出错,可能wsdl 文件中有一段

<s:element ref="s:schema"/><s:any/>

编译到这里的时候就会抛出异常,那么就需要把这个修改成<s:any minOccurs="2" maxOccurs="2"/>即可

2.调用方法时, 返回值是一个对象, 没办法很好的转换成一个xml格式数据,这块把我搞得头大,接下来我就看返回值究竟是个啥, 发现是一个ElementNSImpl,这玩意百度都没说明,但是还是各种查,不管了,继续往下做,最后还是把这个对象解析出来了,心酸。

com.sun.org.apache.xerces.internal.dom.ElementNSImpl eleResult = (ElementNSImpl) result.getAny().get(1);
NodeList nodes = eleResult.getElementsByTagName("Table");

 

获取WebService接口数据有几种方式

1.简单来说http请求

        List<String> results = null;
		try {
			//地址
	        //String urlString = "http://ip:port/ProjectWebService.asmx";
	        //方法
	        String soapActionString = "http://tempuri.org/"+functionName;
	        URL url = new URL(urlString);
	        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
	        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
	                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
	                + "				xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
	                + "				xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
	                "    <soap:Body>\n" +
	                "        <"+functionName+" xmlns=\"http://tempuri.org/\" />\n" +
	                "    </soap:Body>\n" +
	                "</soap:Envelope>";
	        byte[] buf = soap.getBytes();
	        //设置一些头参数
	        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
	        httpConn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
	        httpConn.setRequestProperty("soapActionString", soapActionString);
	        httpConn.setRequestMethod("POST");
	        //输入参数和输出结果
	        httpConn.setDoOutput(true);
	        httpConn.setDoInput(true);
	        OutputStream out = httpConn.getOutputStream();
	        out.write(buf);
	        InputStream in = httpConn.getInputStream();
	        results = dom4jParseXMLByDefaultPath(in,functionName);
	        
	        out.close();
	        in.close();
	        httpConn.disconnect();
		} catch (Exception e) {
			throw(e);
		}
		return results;
    public static List<String> dom4jParseXMLByDefaultPath(InputStream inStream, String                     fuctionName) throws Exception{
    	SAXReader reader = new SAXReader(); 
    	final List<String> strs = new ArrayList<String>();
    	reader.addHandler("", new ElementHandler() {
			@Override
			public void onStart(ElementPath elementPath) {
			}
			@Override
			public void onEnd(ElementPath elementPath) {
				
				Element table = elementPath.getCurrent(); 
        		
				String jsonStr = getNodes(table);
				
				strs.add(jsonStr);
			}
		});
    	reader.read(inStream); 
    	return strs;
    	
    }

    public static String dom4jParseXML(String xmlStr) throws Exception{
		byte[] b = xmlStr.getBytes("UTF-8"); 
        String xml = new String(b,3,b.length-3,"UTF-8");  
		Document doc = DocumentHelper.parseText(xml);
    	return getNodes(doc.getRootElement());
    }
	
	public static String dom4jParseXMLByURL(String url) throws Exception{
		SAXReader reader = new SAXReader();
		Document doc = reader.read(url);
    	return getNodes(doc.getRootElement());
    }

    /**
	 * 获取子结点的内容
	 * 
	 * @param children
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public static String getNodes(Element curEle){
		StringBuilder str = new StringBuilder();
		// 递归遍历当前节点所有的子节点
		List<Element> eles = curEle.elements();
		if (null != eles && eles.size() > 0) {
			str.append("{\"" + curEle.getName() + "\" : {");
			for (int i = 0; i < eles.size(); i++) {
				Element ele = eles.get(i);

				if (i != eles.size() - 1) {
					str.append(getNodes(ele) + ",");
				} else {
					str.append(getNodes(ele));
				}
			}
			str.append("}}");
		} else {
			// 如果没有子节点
			str.append("\"" + curEle.getName() + "\" : \"" + curEle.getText().replace("\"", "\\\"") + "\"");
		}
		return str.toString();
	}

2.第二种下载wsdl源码到本地的方式,获取到的是一个xml对象,可以通过

com.sun.org.apache.xerces.internal.dom.ElementNSImpl eleResult = (ElementNSImpl) result.getAny().get(1);
NodeList nodes = eleResult.getElementsByTagName("Table");

这样的方式一个个遍历得到节点。相比之下还是第一种更高效一点。欢迎大佬来指点

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值