使用Java发送Soap请求

时间:2018-04-01 18:45:06

标签: java web-services soap

我从未使用过Soap。 I searched and found some examples

我的目标是发送这样的Soap请求

<soapenv:Envelope 
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
  <tem:pay>
     <tem:merchantId>7507231</tem:merchantId>
     <tem:branch>Licensed Branch Name</tem:branch>
     <tem:alias>Service alias Name</tem:alias>
     <tem:paymentId>merchants payment idetificator</tem:paymentId>
     <tem:data>
        <tem:param>
           <tem:key>account</tem:key>
           <tem:value>account cridentials</tem:value>
        </tem:param>
     </tem:data>
     <tem:hash>?</tem:hash>
   </tem:pay>
 </soapenv:Body>
</soapenv:Envelope>

有谁能告诉我如何发送这样的Soap请求?或者给我一个示例或教程来发送像这样的肥皂。 谢谢大家

1 个答案:

答案 0 :(得分:1)

下面是一个如何做到这一点的演示。基本上,您为所需的每个元素调用addChildElementaddTextNode

在调用之前,请确保main方法中更改端点URL和SOAP操作

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    // SAAJ - SOAP Client Testing
    public static void main(String args[]) {
        String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx"; // CHANGE ME
        String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit"; // CHANGE ME

        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "tem";
        String myNamespaceURI = "http://tempuri.org/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("pay", myNamespace);

        SOAPElement merchantId = soapBodyElem.addChildElement("merchantId", myNamespace);
        merchantId.addTextNode("7507231");

        SOAPElement branch = soapBodyElem.addChildElement("branch", myNamespace);
        branch.addTextNode("Licensed Branch Name");

        SOAPElement alias = soapBodyElem.addChildElement("alias", myNamespace);
        alias.addTextNode("Service alias Name");

        SOAPElement paymentId = soapBodyElem.addChildElement("paymentId", myNamespace);
        paymentId.addTextNode("merchants payment idetificator");

        SOAPElement data = soapBodyElem.addChildElement("data", myNamespace);
        SOAPElement dataParam = data.addChildElement("param", myNamespace);
        SOAPElement dataParamKey = dataParam.addChildElement("key", myNamespace); dataParamKey.addTextNode("account");
        SOAPElement dataParamValue = dataParam.addChildElement("value", myNamespace); dataParamValue.addTextNode("account cridentials");

        SOAPElement hash = soapBodyElem.addChildElement("hash", myNamespace);
        hash.addTextNode("?");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapRequest = createSOAPRequest(soapAction);
            SOAPMessage soapResponse = soapConnection.call(soapRequest, soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

}