soap报文略说
来源:互联网 发布:c语言比较字符串大小 编辑:程序博客网 时间:2024/05/21 09:40
简单说一下soap报文,有两种版本的报文的报头。
SOAP_1_1_PROTOCOL与SOAP_1_2_PROTOCOL
分别对应的是
1.1:<SOAP-ENV:Envelope xmlns:SOAP ENV="http://schemas.xmlsoap.org/soap/envelope/">1.2:<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body/></env:Envelope>
以下附带一个报文拼装基类。当时写的比较粗略没有写上解析响应报文的通用方法,只有一个接口的解析响应方法。
这个以后有空填坑
/** * 封装soap报文基类 * 1.1:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> * 1.2:<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body/></env:Envelope> * @author huangtao *创建时间2016年9月2日下午9:07:42 * */public class BaseSOAPMessage { //内部维护的报文消息类 private SOAPMessage message; //协议(PROTOCOL1_1,PROTOCOL1_2) private String protocol; //方法名 private String option; //名称空间 private String namespace; //缩写,前缀 private String prefix; //报文节点(节点名称,值) private LinkedHashMap<String,String> elements; final private static String PROTOCOL1_1=SOAPConstants.SOAP_1_1_PROTOCOL; final private static String PROTOCOL1_2=SOAPConstants.SOAP_1_2_PROTOCOL; /** * 默认创建1_1版本的soap协议报文,option,namespace,prefix参数若都设置了则返回一个含参报文 * @return * @throws SOAPException *创建时间:2016年9月2日下午10:04:39 *@author:huangtao */ public SOAPMessage buildBaseSOAPMessage() throws SOAPException{ MessageFactory messageFactory = MessageFactory.newInstance(); message = messageFactory.createMessage(); if((!StringUtils.isEmpty(option)&&!StringUtils.isEmpty(namespace)&&!StringUtils.isEmpty(prefix))){ message=addSOAPMessageElement(); } setMessage(message); return message; } /** * 指定协议版本生成基础报文,option,namespace,prefix参数若都设置了则返回一个含参报文 * @param protocol * @param PROTOCOL1_1为1.1的soap协议,PROTOCOL1_2为1.2的soap协议. * @return * @throws SOAPException *创建时间:2016年9月2日下午10:02:50 *@author:huangtao */ public SOAPMessage buildBaseSOAPMessage(String protocol ) throws SOAPException{ message = (protocol.equals("PROTOCOL1_1"))?(MessageFactory.newInstance(PROTOCOL1_1).createMessage()):(MessageFactory.newInstance(PROTOCOL1_2).createMessage()); if(((!StringUtils.isEmpty(option)&&!StringUtils.isEmpty(namespace)&&!StringUtils.isEmpty(prefix)))){ message=addSOAPMessageElement(); } setMessage(message); return message ; } /** * 将报文转成字节流 * @param message * @return *创建时间:2016年9月4日下午12:20:10 *@author:huangtao */ public byte[] getSOAPMessageAsBytes( ) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { message.writeTo(byteStream); } catch (SOAPException ex) { } catch (IOException ex) { } return byteStream.toByteArray(); } /** * 将报文转成字符串 * @param message * @return *创建时间:2016年9月2日下午9:14:47 *@author:huangtao */ public String getSOAPMessageAsString() { return new String(getSOAPMessageAsBytes( )); } /** * 为soap报文添加节点与值,默认排序以put的先后加入xml节点中 * @param message * @param elements * @return *创建时间:2016年9月2日下午10:49:32 *@author:huangtao * @throws SOAPException */ public SOAPMessage addSOAPMessageElement() throws SOAPException{ //通过SOAPEnvelope有效获取相应的Body和Header等信息 SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); envelope.setAttribute( "xmlns:"+prefix, namespace); SOAPBody body = envelope.getBody(); QName eName = new QName(prefix+":"+option); SOAPBodyElement element = body.addBodyElement(eName); if(elements!=null){ for(Map.Entry<String, String> entry:elements.entrySet()){ element.addChildElement(entry.getKey()).setValue(entry.getValue()); } return message;} return message; } /** *获取接口返回报文中return 的值 * @param soapString * @return */ public static String getResultFromXml(String soapString,String operation) { MessageFactory msgFactory; String result = null; try { msgFactory = MessageFactory.newInstance(); SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(), new ByteArrayInputStream(soapString.getBytes("UTF-8"))); reqMsg.saveChanges(); SOAPBody body = reqMsg.getSOAPBody(); Iterator<SOAPElement> iterator = body.getChildElements(); while (iterator.hasNext()) { SOAPElement element = iterator.next(); String nodeName = "ns2:"+operation+"Response"; if(nodeName.equals(element.getNodeName())){ Iterator<SOAPElement> iterator1 = element.getChildElements(); while (iterator1.hasNext()) { SOAPElement elementr = iterator1.next(); if("return".equals(elementr.getNodeName())){ result = elementr.getValue(); } } } } return result; } catch (Exception e) { e.printStackTrace(); return null; } } public SOAPMessage getMessage() { return message; } public void setMessage(SOAPMessage message) { this.message = message; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public String getOption() { return option; } public void setOption(String option) { this.option = option; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public LinkedHashMap<String, String> getElements() { return elements; } public void setElements(LinkedHashMap<String, String> elements) { this.elements = elements; }public static void main(String[] args) {}}
具体想知道soap报文确切格式可以下载soapui测试接口地址。就可以看到请求和响应的报文内容。
附带官网地址:https://www.soapui.org/
0 0
- soap报文略说
- 略说HTTP协议(二:报文格式和状态码)
- 略说Oracle数据库
- 心经略说
- soap报文格式
- SOAP XML报文解析
- dom4j解析soap报文
- 深入浅出说SOAP
- SOAP请求、响应报文格式
- SOAP请求、响应报文格式
- 智慧人生之禅宗略说
- 《略说USB Key》之前言概述
- 密码学基础知识(八)略说数字签名
- WebService soap报文请求与响应报文解析
- 利用WSE 加密SOAP报文(1)
- 利用WSE 加密SOAP报文(2)
- 利用WSE 加密SOAP报文(3)
- 利用WSE 加密SOAP报文(4)
- shutil.rmtree 函数 OSError: [Errno 39] Directory not empty: 错误原因
- ORA-12638: 身份证明检索失败
- 第四章 数据的类型
- 基于redis实现tomcat8及以上版本的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)
- USB枚举
- soap报文略说
- 初识Android项目知识点积累
- 欢迎使用CSDN-markdown编辑器
- Maven常用命令
- ASP.NET MVC 单点登录(Single Signe On)
- libjpeg-turbo使用实例(编解码jpeg、jpg转bmp、bmp转jpg代码)
- 常用手机uc浏览器兼容问题记录(陆续补充)
- 关于菜单使用gridView来实现的一些小经验
- SecureCRT常用指令