java - 如何在java中准备SOAP请求并调用Web服务

标签 java web-services soap

我是 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);

最佳答案

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

看看this教程。

关于java - 如何在java中准备SOAP请求并调用Web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329327/

相关文章:

java - 使用 Byte Buddy 创建自定义方法签名的高效方法

java - 从 Swing GUI 编译和运行 Java 代码

xml - 使用 Matlab 发送 SOAP 请求

java - 如何在Web服务中添加身份验证 header ?

java - 有没有办法获得最高分的物体

java - RestTemplate 映射到对象

c# - 为网络服务动态构建数组

c# - 如何将 header 中的用户名/密码传递给 SOAP WCF 服务

c# - 在 asp.net core 中实现 SOAP 1.2 服务

java - 为什么request.getParamether在jsp中不起作用