最近在做项目的时候,需要用到 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");
这样的方式一个个遍历得到节点。相比之下还是第一种更高效一点。欢迎大佬来指点