SAAj SOAP客户端

时间:2014-12-18 15:10:48

标签: soap jdk1.5 saaj

我正在使用JDK 1.5和SAAJ [saaj-api-1.3.jar和saaj-impl-1.3.15.jar]和activation.jar

现在我有一个简单的客户端:运行这个我只是得到响应就像ERROR标记没有别的,它非常混乱,我认为webservice有问题,所以我打印了SAAJ生成的SOAP_MESSAGE和使用SOAP-UI发送完全相同的消息,它给了我正确的响应,我甚至尝试了另一个Web服务 网址[http://www.actionscript.org/forums/showthread.php3?t=70742],它似乎正常工作。请有人告诉我,我完全迷失在这里。提前谢谢。

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
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;
import javax.xml.transform.OutputKeys;

public class Test {
    // Method for creating the SOAP Request
    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        // Construct SOAP Request Message:
        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("sch", "http://www.cpscreen.com/schemas");
        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("CPLinkRequest","sch");
        QName attributeName1 = new QName("account");
        soapBodyElem.addAttribute(attributeName1, "NOTEST");
        QName attributeName2 = new QName("userId");
        soapBodyElem.addAttribute(attributeName2, "NONAME");
        QName attributeName3 = new QName("password");
        soapBodyElem.addAttribute(attributeName3, "NOPASSWORD");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Type", "sch");
        soapBodyElem1.addTextNode("Report");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("ProviderReferenceId", "sch");
        soapBodyElem2.addTextNode("WPS-6472130");
        soapMessage.saveChanges();
        // Check the input
        System.out.println("Request SOAP Message for Product web service");
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }

    // Method for receiving the SOAP Response
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.println("\nResponse SOAP Message from Product web service : ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

    // Starting point for SaajClient
    public static void main(String args[]) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            // Sending SOAP Message to SOAP Server i.e, Product Catalog service
            //String url = "http://www.webservicex.net/convertFrequency.asmx?WSDL";
            java.net.URL endpoint = new URL("https://abc.xyz.com/pub/aaa/ws/backgroundCheck");

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(),endpoint);
            // Processing the SOAP Response
            printSOAPResponse(soapResponse);


            //System.out.print("Response SOAP Message:");
            //soapResponse.writeTo(System.out);

            soapConnection.close();


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

0 个答案:

没有答案