如何通过java向服务器发送带参数的soap请求

时间:2014-06-20 13:10:15

标签: java soap

我需要在服务器上发送以下soap请求:

<CallengeState xmlns="http://app.test.com/ws/schema">
 <number>0008</number>
 <previousState>Stopped</previousState>
 <currentState>Activated</currentState>
 <dateChanged>2014-06-09</dateChanged>
</CallengeState >

我知道如何通过Soap Ui来做,但我需要通过java来做到这一点。在Soap UI中我还指定了其他参数,这里是截图:enter image description here

我在google中找到了并尝试了以下内容:

package com.oberthur.tests.util;

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

/**
 * Starting point for the SAAJ - SOAP Client Testing
 */
public static void main(String args[]) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "myserver";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // Process the SOAP Response
        printSOAPResponse(soapResponse);

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

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

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();

    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("CallengeState");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("number");
    soapBodyElem1.addTextNode("00008");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("previousState");
    soapBodyElem2.addTextNode("Stoped");
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("currentState");
    soapBodyElem3.addTextNode("Activated");
    SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("dateChanged");
    soapBodyElem4.addTextNode("2014-06-09");


    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
    System.out.println();

    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();
    System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
}

}

但我不知道如何在此代码中设置我的参数,并且出现以下错误:

Response SOAP Message = [Fatal Error] :2:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
Error occurred while sending SOAP Request to Server

0 个答案:

没有答案