天天看點

用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:

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,如需轉載請自行聯系原作者