【问题标题】:How to prepare SOAP Request and call web service in java如何在 Java 中准备 SOAP 请求和调用 Web 服务
【发布时间】:2014-06-21 02:29:36
【问题描述】:

我是 SOAP 新手,我想调用 Web 服务。
我看过一个制作soap请求的教程。
这是代码

    MessageFactory mf = null;
    SOAPMessage sm = null;
    SOAPEnvelope envelope = null;
    MimeHeaders headers = null;
    SOAPBody body = null;
    SOAPElement requestData = null;
    SOAPElement requestDoc = null;
    CDATASection cdata = null;
    SOAPConnectionFactory sfc = null;
    SOAPConnection connection = null;
    URL requestUrl = null;
    SOAPMessage response = null;
    OutputStream os = null;
    String host = null;
    int port = 80;
    String path = null;
    String api_key = null;
    int socket_timeout = 0;
    String service_type = null;
    String baseXml = null;

    try {

        xmlChannelRequest = createChannelRequest(cmId, function, guId, password, estabId);

        mf = MessageFactory.newInstance();
        sm = mf.createMessage();
        envelope = sm.getSOAPPart().getEnvelope();
        envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.setPrefix("soapenv");

        envelope.setAttribute("xmlns:tem", "http://tempuri.org/");

        headers = sm.getMimeHeaders();
        headers.addHeader("SOAPAction", "http://tempuri.org/IPublicChannelManagerService/RequestData");

        body = envelope.getBody();
        body.setPrefix("soapenv");

        requestData = body.addChildElement("RequestData");
        requestData.setPrefix("tem");

        requestDoc = requestData.addChildElement("requestDocument", "tem", "http://tempuri.org/");


     cdata = requestDoc.getOwnerDocument().createCDATASection("<Request>\n"
                + "           <Authentication CMId=\"6438\" Function=\"31\" Guid=\"5594FB83-F4D4-431F-B3C5-EA6D7ASDSBA795\" Password=\"y656g321TR\"/>\n"
                + "           <Establishment Id=\"297867\"/>\n"
                + "        </Request>");



        requestDoc.appendChild(cdata);

        System.out.println("----------------  SOAP Request ----------------------------");

        sm.writeTo(System.out);

        sfc = SOAPConnectionFactory.newInstance();
        connection = sfc.createConnection();
        requestUrl = new URL("http://pp.xxx.yyyy.co.uk/zzzz.svc?wsdl");

        response = connection.call(sm, requestUrl);

这工作正常。我的问题是,有没有简单的方法,就像将对象传递给方法并创建它一样,SOAP 请求。
ex : 就像 JaxB 一样,我会发送一个 Bean,它会生成 xml。

        outputStream = new ByteArrayOutputStream();

        marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(beanObject, outputStream);

【问题讨论】:

    标签: java web-services soap


    【解决方案1】:

    在发出请求之前,您应该拥有WSDL 文件。然后只需使用例如生成您的特定类wsimport 工具,然后只需调用 JAX-WS 网络服务。您不必以这种方式自己构建请求

    查看this 教程。

    【讨论】:

      猜你喜欢
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      相关资源
      最近更新 更多