如何在java中创建soap xml请求

时间:2017-05-29 09:33:28

标签: java xml soap

我是肥皂新手。我无法发出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请求。有人可以帮忙......

1 个答案:

答案 0 :(得分:0)

<system.web>
    <httpRuntime targetFramework="4.5.1" requestValidationMode="2.0" />
    ...
</system.web>