java soap封装_Java SOAPMessage.writeTo方法代码示例

import javax.xml.soap.SOAPMessage; //导入方法依赖的package包/类

private byte[] createProbeXML() throws SOAPException, IOException {

MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

SOAPMessage message = messageFactory.createMessage();

SOAPPart part = message.getSOAPPart();

SOAPEnvelope envelope = part.getEnvelope();

envelope.addNamespaceDeclaration("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");

envelope.addNamespaceDeclaration("tns", "http://schemas.xmlsoap.org/ws/2005/04/discovery");

envelope.addNamespaceDeclaration("nns", "http://www.onvif.org/ver10/network/wsdl");

QName action = envelope.createQName("Action", "wsa");

QName mid = envelope.createQName("MessageID", "wsa");

QName to = envelope.createQName("To", "wsa");

QName probe = envelope.createQName("Probe", "tns");

QName types = envelope.createQName("Types", "tns");

QName tramsmitter=envelope.createQName("NetworkVideoTransmitter", "nns");

SOAPHeader header = envelope.getHeader();

SOAPElement actionEl = header.addChildElement(action);

actionEl.setTextContent("http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe");

SOAPElement messIsEl = header.addChildElement(mid);

messIsEl.setTextContent("urn:uuid:" + UUID.randomUUID().toString());

SOAPElement toEl = header.addChildElement(to);

toEl.setTextContent("urn:schemas-xmlsoap-org:ws:2005:04:discovery");

SOAPBody body = envelope.getBody();

SOAPElement probeEl = body.addChildElement(probe);

SOAPElement typesEl=probeEl.addChildElement(types);

typesEl.setTextContent("nns:NetworkVideoTransmitter");

ByteArrayOutputStream out = new ByteArrayOutputStream();

message.writeTo(out);

return out.toByteArray();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用SOAP API来操作和发送SOAP消息。下面是一个简单的示例: 首先,需要引入以下依赖: ``` <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>javax.xml.soap-api</artifactId> <version>1.4.0</version> </dependency> ``` 然后,可以使用以下代码创建SOAP消息: ``` // 创建SOAP消息工厂 MessageFactory factory = MessageFactory.newInstance(); // 创建SOAP消息 SOAPMessage message = factory.createMessage(); // 获取SOAP消息体 SOAPBody body = message.getSOAPBody(); // 创建命名空间 String namespaceURI = "http://www.example.org/stock"; String prefix = "stock"; body.addNamespaceDeclaration(prefix, namespaceURI); // 创建请求消息元素 SOAPElement requestElem = body.addChildElement("getStockPrice", prefix); // 添加请求参数 SOAPElement symbolElem = requestElem.addChildElement("symbol", prefix); symbolElem.addTextNode("AAPL"); // 发送SOAP消息 String endpointUrl = "http://www.example.org/stockquote"; SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = connectionFactory.createConnection(); SOAPMessage response = connection.call(message, endpointUrl); connection.close(); // 解析响应消息 SOAPBody responseBody = response.getSOAPBody(); SOAPElement responseElem = (SOAPElement) responseBody.getChildElements().next(); double stockPrice = Double.parseDouble(responseElem.getValue()); ``` 在上面的示例中,我们首先创建了一个SOAP消息工厂,然后使用它来创建一个SOAP消息。接着,我们获取了SOAP消息体,并添加了一个命名空间和请求消息元素。在请求消息元素中,我们添加了一个请求参数。最后,我们使用SOAP连接工厂和连接来发送SOAP消息,并解析响应消息。 需要注意的是,上面的示例只是一个简单的示例,实际使用中可能需要更复杂的SOAP消息和处理机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值