最近在做項目的時候,需要用到 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");
這樣的方式一個個遍歷得到節點。相比之下還是第一種更高效一點。歡迎大佬來指點