如果你的webService 地址直接类似于(天气预报:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)这样的:
点击你要解析的方法,进入如下:
然后使用HttpClient 解析webService :
package com.example.util; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.List; /** * Created by xjw on 2017/6/29. */ public class WebServiceUtil { private static Logger log = LoggerFactory.getLogger("webServiceUtil-log"); private static int socketTimeOut = 30000;//请求超时时间 private static int connectTimeOut = 30000;//传输超时时间 public static void main(String[] args){ String storeInfoUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; String contentType = "text/xml; charset=utf-8";//soap1.1 的conetentType String soapAction = "http://WebXml.com.cn/getSupportCity"; // String soapAction =""; // String contentType = "application/soap+xml; charset=utf-8"; //soap1.2 的conetentType String ProvinceName = "上海"; String soapXml = "<?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" + " <getSupportCity xmlns=\"http://WebXml.com.cn/\">\n" + " <byProvinceName>"+ProvinceName+"</byProvinceName>\n" + " </getSupportCity>\n" + " </soap:Body>\n" + "</soap:Envelope>"; InputStream input = WebServiceUtil.soap(storeInfoUrl, soapXml, soapAction, contentType); //下面是解析 返回的 XML SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(input); Element root = document.getRootElement(); Dom4jUtils.getNodes(root); Element body = root.element("Body"); Element getSupportCityResponse = body.element("getSupportCityResponse"); Element getSupportCityResult = getSupportCityResponse.element("getSupportCityResult"); List<Element> strings = getSupportCityResult.elements(); for(Element e : strings){ System.out.println(e.getTextTrim()); } } catch (DocumentException e) { e.printStackTrace(); } } public static InputStream soap(String url,String soapXml,String soapAction,String ContentType) { InputStream soapResponseData = null; // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(url); //设置请求超时时间 和 传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut) .setConnectTimeout(connectTimeOut).build(); httpPost.setConfig(requestConfig); httpPost.setHeader("Content-Type",ContentType); httpPost.setHeader("SOAPAction",soapAction); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = null; try { response = closeableHttpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity !=null){ //打印响应内容 byte[] bytes = EntityUtils.toByteArray(httpEntity); soapResponseData =new ByteArrayInputStream(bytes); log.info("---请求webService之后的返回值是----"+new String(bytes)); } } catch (IOException e) { e.printStackTrace(); }finally { try { closeableHttpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return soapResponseData; } }
如果 接口地址是 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 带 ?wsdl
需要使用 soapUI 等工具:
SoapUI工具下载地址:链接:http://pan.baidu.com/s/1hrAyDaG 密码:ah3m
然后后面的的解析和上面的一样。