如何使用用于 XML 消息传递的 Java API(Java API for XML Messaging (JAXM))简化创建和发送 SOAP 消息的过程。
Web 服务的基础在于以标准格式发送和接收消息以便使所有系统都能理解。通常,那种格式是简单对象访问协议(Simple Object Access Protocol (SOAP))。SOAP 消息可以手工生成和发送,但是用于 XML 消息传递的 Java API(JAXM)使许多必需步骤(如创建连接或创建并发送实际消息)自动化。
这个过程包含五个步骤:
创建 SOAP 连接
创建 SOAP 消息
填充消息
发送消息
检索应答
一个基本的 SOAP 消息由包含两个主要部分(报头和主体)的封套组成。应用程序决定如何使用这些部分,但整个消息必须遵循特定的XML 结构(soap.msg文件
):
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:SayHello xmlns:ns1="http://boomga.com">
</ns1:SayHello>
</soap:Body>
</soap:Envelope>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:SayHello xmlns:ns1="http://boomga.com">
</ns1:SayHello>
</soap:Body>
</soap:Envelope>
请注意这个消息的结构。Envelope 包含Body 元素,而二者全都是 http://schemas.xmlsoap.org/soap/envelope/ 名称空间的一部分。整个消息将通过一个 SOAP 连接发送到一个 Web 服务中。
public static void doSoapPost()
{
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 Message
StreamSource preppedMsgSrc = new StreamSource(
new FileInputStream("E:\\soap.msg"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination =
"http://localhost:8000/HelloWorld/services/HelloWorldService";
//Send the message
SOAPMessage reply = connection.call(message, destination);
// Check the output
System.out.println("\nRESPONSE:\n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Close the connection
connection.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
{
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 Message
StreamSource preppedMsgSrc = new StreamSource(
new FileInputStream("E:\\soap.msg"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination =
"http://localhost:8000/HelloWorld/services/HelloWorldService";
//Send the message
SOAPMessage reply = connection.call(message, destination);
// Check the output
System.out.println("\nRESPONSE:\n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Close the connection
connection.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
运行结果:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:SayHello xmlns:ns1="http://boomga.com">
</ns1:SayHello>
</soap:Body>
</soap:Envelope>
RESPONSE:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:SayHelloResponse xmlns:ns1="http://boomga.com"><ns1:out>dyk,Hell0</ns1:out></ns1:SayHelloResponse></soap:Body></soap:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:SayHello xmlns:ns1="http://boomga.com">
</ns1:SayHello>
</soap:Body>
</soap:Envelope>
RESPONSE:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:SayHelloResponse xmlns:ns1="http://boomga.com"><ns1:out>dyk,Hell0</ns1:out></ns1:SayHelloResponse></soap:Body></soap:Envelope>
相关推荐
使用Soap消息调用Web Services
c#动态调用Webservices 不用VS生成 简单快捷
基于 .NET, Java and MS SOAP Toolkit进行Web Services 的客户端调用程序例子
C++调用Web services的控制台应用程序 C++调用Web services的控制台应用程序
http post方式请求 webservices soap协议接口
这里介绍的Web Services JavaScript Library扩展了该基础机制,其通过引入对调用基于SOAP的Web服务的支持来增强Ajax设计模式。本文是系列文章的第1部分,演示了如何使用针对Web应用程序的Ajax设计模式来实现跨平台的...
Web Services 教程 5 内容目录 5 Web Services 简介 6 您应当具备的基础知识 6 什么是Web Services? 6 它如何工作? 6 Web services 的未来 6 Why Web Services? 7 最重要的事情是协同工作 7 Web services 把 Web ...
本示例使用C#构造SOAP信息,通过HttpWebRequest调用java编写的带有Windows身份验证的WebService,代码中详细注释了每行代码的功能与作用; 对应文章:http://blog.csdn.net/cgs_______/article/details/77894599
在本系列的第1部分“使用Ajax调用SOAP Web服务,第 1 部分”中,作者引入了一个简单的用于调用SOAP Web服务的基于Web浏览器的JavaScript库。在接下来的讨论中,作者将实现对Web服务寻址语言 (Web Services ...
封装了java调用.net的websevice接口,解决了soapaction报错和server not identified错误,使用soap1.1协议。
SOAP是Web services的传输层协议,所有的SOAP消息均使用XML编码。XML engine是建立在Web service上的一种技术。在整个Web服务应用中,XML处于消息层的位置,而XML engine则针对Web服务应用中的XML进行操作,所以称做...
C#开发核心代码,用拼接XML方式,调用封装了实体类的SOAP接口
Soap WebService 调试工具
根据用户输入的参数,axis2使用底层API,根据访问入口点构造soap消息,实现了特定服务的同步调用和异步调用,同时还实现了带附件的服务调用。 本系统通过以上过程,实现服务调用框架,拓展了面向服务的应用。同时,...
使用soap协议进行不同语实验的系统是webservice的重要优势。本文档给出了怎么使用vc++ 6.0调用webservice的例子。
webservice工具,针对wsdl、soap进行测试的小软件
什么是WebServices? 它是一种构建应用程序的普遍模型,可以在任何支持网络通信的操作系统中实施运行;它是一种新的web应用程序分支,是自包含、自描述、模块化的应用,可以发布、定位、通过web调用。Web Service...
关于此代码的使用及相关配置,可以见此文:http://blog.csdn.net/u012995964/article/details/54562111 代码开发时环境:Windows8.1、Visual Studio 2013、IIS8 注意:测试代码时,重新部署WebService服务且修改...