webService发送soap请求,并解析返回的soap报⽂
本例应⽤场景:要做⼀个webService测试功能,不局限于任何⼀种固定格式的webService,所以像axis,cxf等框架就不好⽤了。只有深⼊到webService的原理,通过发收soap报⽂,来调⽤服务返回结果。
发送请求:
/**
* 通过httpClient发送soap报⽂
* @param requestSoap 请求报⽂
* @param serviceAddress 请求地址
* @param charSet 字符集
* @param contentType 返回的contentType
* @return响应报⽂
* @throws WebServiceModuleRuntimeException
*/
public String sendRequestSoap(String requestSoap, String serviceAddress, String charSet, String contentType)
throws WebServiceModuleRuntimeException {
String resultSoap = "";
PostMethod postMethod = new PostMethod(serviceAddress);
byte[] b = new byte[0];
try {
b = Bytes(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 = uteMethod(postMethod);
System.out.println("statusCode = " + statusCode);
} catch (IOException e) {
throw new WebServiceModuleRuntimeException("执⾏http请求失败", e);
}
if (statusCode == 200) {
try {
resultSoap = ResponseBodyAsString();
} catch (IOException e) {
throw new WebServiceModuleRuntimeException("获取请求返回报⽂失败", e);
}
} else {
throw new WebServiceModuleRuntimeException("请求失败:" + statusCode);
}
return resultSoap;
}
//调⽤请求⽅法,发送报⽂
String responseSoap = "";
try{
responseSoap = webServiceService.sendRequestSoap(WebAddress(),"utf-8","text/xml; charset=utf-8");
}catch (WebServiceModuleRuntimeException ex){
throw new ModuleException("发动请求失败",ex);
}
解析返回报⽂:
因没有固定格式,所以⽆法通过jaxb⼯具来xml转bean,更没有客户端代码可以⽤。所以只有解析返回报⽂中,可以标识返回结果的值,⽐如成功、success、ok等。
此处考虑两种情况:第⼀种状态码放在标签的属性值中,第⼆种状态作为标签的内容:
<result ResultCode="0" ResultCodeDesc="成功">
<result_code>0</result_code>
System.out.println(parseResponseSoap("result_code", "", responseSoap));
/**
* 解析返回报⽂
* @param node 标记所在节点
* @param attr 标记所在属性
* @param soap 报⽂
* @return标记值
* @throws WebServiceModuleRuntimeException
*/
public static String parseResponseSoap(String node, String attr, String soap) throws WebServiceModuleRuntimeException {
调用webservice服务//然后⽤SOAPMessage 和 SOAPBody
Document personDoc;
try {
personDoc = new SAXReader().read(new StringReader(soap));
Element rootElt = RootElement(); // 获取根节点
Iterator body = rootElt.elementIterator("Body");
while (body.hasNext()) {
Element recordEless = (Element) ();
return nextSubElement(node,attr,recordEless);
}
} catch (DocumentException e) {
throw new WebServiceModuleRuntimeException("解析返回报⽂失败", e);
}
return "";
}
/**
* 递归⽅法,查本节点是否有标记信息,如果没有就查下⼀层,
* 在下⼀层⾥同样查本层节点,只要到值,就层层返回。
* @param node 节点标签名
* @param attr 节点属性值
* @param el 当前节点对象
* @return⽬标值
*/
public static String nextSubElement(String node, String attr, Element el) {
if (el.getName().equals(node)) {
//说明到了⽬标节点
//属性值为空说明取标签内容
if (attr.equals("")) {
Iterator sub2 = el.elementIterator();
//有⼦节点说明标签内容不是单⼀值,需要拿到查询结果
if (sub2.hasNext()) {
while (sub2.hasNext()) {
Element s2 = (Element) ();
//如果返回的不是单⼀的标记值,⽽是查询结果,有些⿇烦,
//查询结果应当是list<map>格式,但是map的key值不好确定,是标签名作为key还是属性值作为key //todo
}
} else {
return el.getText();
}
} else {
Attribute attrbute = el.attribute(attr);
Text();
}
} else {
Iterator sub2 = el.elementIterator();
while (sub2.hasNext()) {
Element sub = (Element) ();
return nextSubElement(node, attr, sub);
}
}
return "";
}
后记:本篇代码满⾜我⾃⼰的需求,但是看官的需求各异,本篇仅提供部分参考。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论