簡體   English   中英

如何在Java中發出soap xml請求

[英]how to make a soap xml request in java

我是肥皂新手。 我無法提出soap xml請求。 所需的XML格式為:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<SendRequest xmlns="http://tempuri.org/"> 
<request xsi:type="CheckStatusRequest" Id="a8bee82b-3c01-44ab-8d56-4728d3c76dd8" xmlns="http://paygo24.com/v3/protocol"/> 
<pointId>46</pointId> 
<password>4QrcOUm6Wau+VuBX8g+IPg==</password> 
</SendRequest> 
</soap:Body> 
</soap:Envelope>

我正在使用的Java代碼,嘗試做出上述xml請求是:

package org.tempuri;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

public class SOAPClientSAAJ {



    public static void main(String[] args) {
        // TODO Auto-generated method stub\

        System.out.println("test");

        try {
            SOAPConnectionFactory connectionFactory = SOAPConnectionFactory
                    .newInstance();
            SOAPConnection soapConnection = connectionFactory
                    .createConnection();

            String url = "https://processing2.paygo24.com/paygoservice.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequestSendRequest(), url);

            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.out
                    .println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }

        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private static SOAPMessage createSOAPRequestSendRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://tempuri.org/";


        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("tem", serverURI);
        envelope.addNamespaceDeclaration("xsi",
                "http://www.w3.org/2001/XMLSchema-instance");
        envelope.addNamespaceDeclaration("xsd",
                "http://www.w3.org/2001/XMLSchema");
        envelope.addNamespaceDeclaration("cash",
                "http://paygo24.com/v3/protocol");

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("SendRequest", "tem");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("request", "cash");

        QName type = new QName("xsi:type");
        QName Id= new QName("Id");

        soapBodyElem1.addAttribute(type, "cash:CheckStatusRequest");
        soapBodyElem1.addAttribute(Id, "6926685"); 


    SOAPElement paymentParameter=soapBodyElem1.addChildElement("PaymentParameters");
    paymentParameter.addAttribute(new QName("xmlns"),"");

        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("pointId",
                "tem");
        soapBodyElem2.addTextNode("46");

        SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("password",
                "tem");
        soapBodyElem3.addTextNode("bTf/TVO7k/mCPzeXSVgJVA==");


        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI + "SendRequest");


        soapMessage.saveChanges();


        ByteArrayOutputStream out = new ByteArrayOutputStream();
        soapMessage.writeTo(out);

        System.out.print("Request SOAP Message ");
        System.out.println(new String(out.toByteArray()));

        return soapMessage;
    }

    private static SOAPMessage createSOAPRequestGetCurrencyRate()
            throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://tempuri.org/";

            SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("tem", serverURI);

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


        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI + "GetCurrencyRate");


        soapMessage.saveChanges();


        ByteArrayOutputStream out = new ByteArrayOutputStream();
        soapMessage.writeTo(out);
        System.out.print("Request SOAP Message");
        System.out.println(new String(out.toByteArray()));
        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse)
            throws Exception {
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        transformer.transform(sourceContent, result);

        System.out.print("Response SOAP Message");
        System.out.println(new String(out.toByteArray()));
    }
}

但是我無法創建上述確切的XML請求。 可以幫忙一下嗎...

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

            SOAPPart soapPart = soapMessage.getSOAPPart();

            String serverURI = "http://tempuri.org/";


            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.removeNamespaceDeclaration("SOAP-ENV");
            envelope.setPrefix("soap");        
            envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
            envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");


            // SOAP Body

            SOAPBody soapBody = envelope.getBody();
            soapBody.setPrefix("soap");
            SOAPElement soapBodyElem = soapBody.addChildElement(new QName("http://tempuri.org/","SendRequest"));
            SOAPElement soapBodyElem1 = soapBodyElem.addChildElement(new QName("http://paygo24.com/v3/protocol","request"));
            soapBodyElem1.addAttribute(new QName("xsi:type"), "CheckStatusRequest");
            soapBodyElem1.addAttribute(new QName("Id"), "a8bee82b-3c01-44ab-8d56-4728d3c76dd8");
            SOAPElement soapBodyElem2 = soapBodyElem.addChildElement(new QName("pointId"));
            soapBodyElem2.addTextNode("46");
            SOAPElement soapBodyElem3 = soapBodyElem.addChildElement(new QName("password"));
            soapBodyElem3.addTextNode("bTf/TVO7k/mCPzeXSVgJVA==");
            MimeHeaders headers = soapMessage.getMimeHeaders();
            soapMessage.getSOAPHeader().detachNode();
            headers.addHeader("SOAPAction", serverURI + "SendRequest");


            soapMessage.saveChanges();


            ByteArrayOutputStream out = new ByteArrayOutputStream();
            soapMessage.writeTo(out);

            System.out.print("Request SOAP Message ");
            System.out.println(new String(out.toByteArray()));

            return soapMessage;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM