用Soap消息调用Web Services

如何使用用于 XML 消息传递的 Java APIJava API for XML Messaging (JAXM))简化创建和发送 SOAP 消息的过程。

Web 服务的基础在于以标准格式发送和接收消息以便使所有系统都能理解。通常,那种格式是简单对象访问协议(Simple Object Access Protocol (SOAP))。SOAP 消息可以手工生成和发送,但是用于 XML 消息传递的 Java APIJAXM)使许多必需步骤(如创建连接或创建并发送实际消息)自动化。

这个过程包含五个步骤:

创建 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>

posted on   Phinecos(洞庭散人)  阅读(3274)  评论(2编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2006-08-17 [翻译]互联网战略:反Google联盟(Internet strategy:The alliance against Google)
2006-08-17 一个清理系统盘垃圾文件的方法

导航

统计

点击右上角即可分享
微信分享提示