用HttpClinet 来获取响应报文,document文档解析响应报文
为了结构明确,层次分明,我将冗长的方法封装在了多个方法中,在主方法中体现功能调用顺序,这样思路更清晰些。大家可以看主方法的中的调用顺序进行学习。调用方法出用红色字体标注.思路由黄色底色标注。
-
public static void main(String[] args) {
-
StringBuilder soap=new StringBuilder(); //构造请求报文
-
soap.append(" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
-
soap.append(" xmlns:wor=\"http://www.horizon.com/workflow/webservice/client/workflowCommon\">");
-
soap.append(" <soapenv:Header>");
-
soap.append(" <HZWFService xmlns=\"http://www.huizhengtech.com/webservice/workflow\"");
-
soap.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"");
-
soap.append(" SOAP-ENV:actor=\"http://www.w3.org/2003/05/soap-envelope/role/next\">admin&admin</HZWFService>");
-
soap.append(" </soapenv:Header>");
-
soap.append(" <soapenv:Body>");
-
soap.append(" <wor:sysLogin>");
-
soap.append(" <loginName>loginname</loginName >");
-
soap.append(" <password>password</password>");
-
soap.append(" <dbidentifier>system</dbidentifier>");
-
soap.append(" </wor:sysLogin>");
-
soap.append(" </soapenv:Body>");
-
soap.append(" </soapenv:Envelope>");
-
String requestSoap=soap.toString();
-
String serviceAddress="http://IP:Port/WorkflowCommonService?wsdl"; //服务地址(将XXXX替换成自己项目的地址)
-
String charSet="utf-8";
-
String contentType="text/xml; charset=utf-8";
-
<span style="font-size:24px;">//第一步:调用方法getResponseSoap。返回响应报文和状态码</span>
-
Map<String,Object> responseSoapMap=SoapUtil.getResponseSoap(requestSoap, serviceAddress, charSet, contentType);
-
Integer statusCode=(Integer)responseSoapMap.get("statusCode");
-
if(statusCode==200){
-
String responseSoap=(String)responseSoapMap.get("responseSoap");
-
String targetNodeName="isSuccess";
-
<span style="font-size:24px;"> //第二步:调用strXmlToDocument方法。
-
将字符串类型的XML的响应报文 转化成Docunent结构文档</span>
-
Document doc=XMLUtil.strXmlToDocument(responseSoap);
-
<span style="font-size:24px;"> //第三步:调用getValueByElementName方法。递归获得目标节点的值</span>
-
String result= XMLUtil.getValueByElementName(doc,targetNodeName);
-
if(!StringUtils.isEmpty(result)){
-
System.out.println(result);
-
}
-
else{
-
System.out.println("没有此节点或者没有值!");}
-
}
-
else{
-
System.out.println("请求失败!");
-
}
-
}
工具类 SoapUtil.class 。
-
/**
-
* <p>Description: 根据请求报文,请求服务地址获取 响应报文
-
* @param requestSoap 请求报文
-
* @param serviceAddress 响应报文
-
* @param charSet 字符集
-
* @param contentType 类型
-
* @return map封装的 服务器响应参数和返回报文.PS:statusCode :200正常响应。responseSoap:响应报文
-
* <p>thinking: </p>
-
public static Map<String,Object> responseSoap(String requestSoap,String serviceAddress,String charSet, String contentType){
-
String responseSoap="";
-
Map<String,Object> resultmap=new HashMap<String,Object>();
-
PostMethod postMethod = new PostMethod(serviceAddress);
-
byte[] b = new byte[0];
-
try {
-
b = requestSoap.getBytes(charSet);
-
} catch (UnsupportedEncodingException e) {
-
e.printStackTrace();
-
}
-
InputStream is = new ByteArrayInputStream(b, 0, b.length);
-
RequestEntity re = new InputStreamRequestEntity(is, b.length, contentType);
-
postMethod.setRequestEntity(re);
-
HttpClient httpClient = new HttpClient();
-
int statusCode = 0;
-
try {
-
statusCode = httpClient.executeMethod(postMethod);
-
resultmap.put("statusCode", statusCode);
-
} catch (IOException e) {
-
throw new RuntimeException("执行http请求失败", e);
-
}
-
if (statusCode == 200) {
-
try {
-
responseSoap = postMethod.getResponseBodyAsString();
-
resultmap.put("responseSoap", responseSoap);
-
} catch (IOException e) {
-
throw new RuntimeException("获取请求返回报文失败", e);
-
}
-
} else {
-
throw new RuntimeException("请求失败:" + statusCode);
-
}
-
return resultmap;
-
}
工具类 XMLUtil.class
-
/**
-
* <p>Description:将字符串类型的XML 转化成Docunent文档结构</p>
-
* @param parseStrXml 待转换的xml 字符串
-
* @return Document
-
*
-
* @author hu
-
*/
-
public static Document strXmlToDocument(String parseStrXml){
-
StringReader read = new StringReader(parseStrXml);
-
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
-
InputSource source = new InputSource(read);
-
//创建一个新的SAXBuilder
-
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
-
Document doc = null;
-
try {
-
doc = sb.build(source);
-
} catch (JDOMException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
return doc;
-
}
-
/**
-
* <p>Description: 根据目标节点名获取值</p>
-
* @param doc 文档结构
-
* @param finalNodeName 最终节点名
-
* @return
-
*
-
* @author h
-
*/
-
public static String getValueByElementName(Document doc,String finalNodeName){
-
Element root = doc.getRootElement();
-
HashMap<String,Object> map=new HashMap<String,Object>();
-
<span style="font-size:24px;"> //调用getChildAllText方法。获取目标子节点的值 </span>
-
Map<String,Object> resultmap=getChildAllText(doc, root,map);
-
String result=(String)resultmap.get(finalNodeName);
-
return result;
-
}
-
/**
-
* <p>Description: 递归获得子节点的值</p>
-
* @param doc 文档结构
-
* @param e 节点元素
-
* @param resultmap 递归将值压入map中
-
* @return
-
*
-
* @author huoge
-
*/
-
public static Map<String ,Object> getChildAllText(Document doc, Element e,HashMap<String,Object> resultmap)
-
{
-
if (e != null)
-
{
-
if (e.getChildren() != null) //如果存在子节点
-
{
-
List<Element> list = e.getChildren();
-
for (Element el : list) //循环输出
-
{
-
if(el.getChildren().size() > 0) //如果子节点还存在子节点,则递归获取
-
{
-
getChildAllText(doc, el,resultmap);
-
}
-
else
-
{
-
resultmap.put(el.getName(), el.getTextTrim()); //将叶子节点值压入map
-
}
-
}
-
}
-
}
-
return resultmap;
-
}
这就是用webservice进行soap接口调用的思路。另外webservice接口调用还可以返回json数据格式然后进行对JSon进行解析,比较容易。