用Soap消息调用Web Services

简介:

如何使用用于 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>

请注意这个消息的结构。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());
        }   
    }

运行结果:
 
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>



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/08/17/859897.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
前端开发 JavaScript API
阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
【1月更文挑战第15天】【1月更文挑战第72篇】阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
58 6
|
XML Java 数据格式
大多数人忽略了的Spring官方项目,Spring Web Services
大多数人忽略了的Spring官方项目,Spring Web Services
995 0
|
2月前
|
XML 安全 数据安全/隐私保护
探索 SOAP:揭开 Web 服务的神秘面纱(下)
探索 SOAP:揭开 Web 服务的神秘面纱(下)
|
2月前
|
XML 开发框架 JSON
探索 SOAP:揭开 Web 服务的神秘面纱(上)
探索 SOAP:揭开 Web 服务的神秘面纱(上)
|
8月前
|
弹性计算 安全 数据安全/隐私保护
Internet Information Services(IIS)部署Web项目
本文为您介绍如何快速使用IIS搭建简单网站并发布项目。
270 0
|
XML 缓存 JSON
REST vs SOAP:两种 Web 服务协议的分析
REST(Representational State Transfer)和 SOAP(Simple Object Access Protocol)都是 Web 服务架构的两种主要风格。两者都提供了一种通信方式,可以让不同的应用程序通过网络互相交换数据。但是,它们之间有一些重要的区别。
|
Java Linux API
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
496 0
|
XML JavaScript 网络协议
WEB SERVICE名词解释 JSWDL开发包的介绍 JAXP、JAXM的解释 SOAP、UDDI,WSDL解释。
Web ServiceWeb Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵守具体的技术规范,这些规范使得Web Service能与其他兼容的组件进行互操作。
70 0
|
XML 网络协议 JavaScript
Web Services:重整山河待后生
Web Services:重整山河待后生
137 0
Web Services:重整山河待后生
|
监控 Java 区块链
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service
251 0
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service