使用SAAJ发送和接收SOAP消息
2010-06-04 18:04
423 查看
Web 服务的基础是以标准格式发送和接收消息(SOAP XML),这样所有系统都可以理解消息.JAVA saaj提供了一组API可以用来创建 SOAP 连接,生成 SOAP 消息,发送请求消息和得到返回消息.
http://blog.csdn.net/kkdelta/archive/2009/03/13/3987591.aspx介绍的proxy方式以及dispatch方式最终也是通过saaj发送和获取返回的.
例子:
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
// Populate the body Create the main element and namespace
//SOAPElement bodyElement = body.addChildElement(envelope.createName("queryPeopleByID", "ns1",
// "http://test.cxfws.com/"));
// Add content
//bodyElement.addTextNode("1231ss");
StreamSource xmlSource1 = new StreamSource(MsgUtil.readMsgAsStream("com/test/jaxws/dispatch/req.xml"));
soapPart.setContent(xmlSource1);
// Save the message
message.saveChanges();
// Check the input
message.writeTo(System.out);
System.out.println();
// Set the destination
String destination = "http://localhost:8080/prjCXFWS/services/SimpleServicePort";
// Send the message
SOAPMessage reply = connection.call(message, destination);
// Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
StreamResult result = new StreamResult(new ByteArrayOutputStream());
transformer.transform(sourceContent, result);
System.out.println(((ByteArrayOutputStream)result.getOutputStream()).toString());
// Close the connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
http://blog.csdn.net/kkdelta/archive/2009/03/13/3987591.aspx介绍的proxy方式以及dispatch方式最终也是通过saaj发送和获取返回的.
例子:
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
// Populate the body Create the main element and namespace
//SOAPElement bodyElement = body.addChildElement(envelope.createName("queryPeopleByID", "ns1",
// "http://test.cxfws.com/"));
// Add content
//bodyElement.addTextNode("1231ss");
StreamSource xmlSource1 = new StreamSource(MsgUtil.readMsgAsStream("com/test/jaxws/dispatch/req.xml"));
soapPart.setContent(xmlSource1);
// Save the message
message.saveChanges();
// Check the input
message.writeTo(System.out);
System.out.println();
// Set the destination
String destination = "http://localhost:8080/prjCXFWS/services/SimpleServicePort";
// Send the message
SOAPMessage reply = connection.call(message, destination);
// Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
StreamResult result = new StreamResult(new ByteArrayOutputStream());
transformer.transform(sourceContent, result);
System.out.println(((ByteArrayOutputStream)result.getOutputStream()).toString());
// Close the connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- : 使用SAAJ发送和接收SOAP消息
- 使用SAAJ发送和接收SOAP消息
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- Android非UI主线程中,若干普通Java线程使用Handler发送接收消息
- 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息
- JMS_使用ActiveMQ实现消息的发送和接收
- Webservice的wsdl文件解析与Soap消息的发送、接收(不生成java客户端代码)
- opensaml的SOAP消息发送和接收
- 使用spring-rabbit测试RabbitMQ消息确认(发送确认,接收确认)
- 使用JMS发送和接收text和Object类型的消息
- QUdpSocket简单使用Demo(局域网内发送给所有客户端消息与接收)
- WebSphereMq使用JMS发送消息和接收消息
- activemq使用JMS发送消息和接收消息
- 使用Akka持久化——消息发送与接收
- ActiveMQ的消息的(含附件)发送和接收使用
- rabbitmq学习9:使用spring-amqp发送消息及同步接收消息
- ASP.NET--使用Socket发送和接收消息[转载]
- 【Spring】使用Spring和AMQP发送接收消息(下)
- ROS_Kinetic_26 使用rosserial_windows实现windows与ROS master发送与接收消息
- IOS使用XMPP最新教程(六)发送和接收消息