用于调用 SOAP - Web 服务的 Java 类

标签 java web-services class soap

我有一个 SOAP,需要从 Oracle 调用,并且听说唯一的解决方法是通过 Java 类,不幸的是,我不熟悉 Java,因为我是 Oracle 开发人员 (Oracle Forms)如果有人可以帮助我创建一个调用此 SOAP 的类,以便我可以在 Oracle 数据库上构建它并以调用函数的方式从 Oracle 表单生成器中调用它,我真的很感激。

有两个 SOAP(1.1 和 1.2),任何一个都可以工作:

*SOAP 1.1

以下是 SOAP 1.1 请求和响应示例。显示的占位符需要替换为实际值。

POST /gmgwebservice/service.asmx HTTP/1.1
Host: 212.35.66.180
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SendSMS"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendSMS xmlns="http://tempuri.org/">
      <UserName>string</UserName>
      <Password>string</Password>
      <MessageBody>string</MessageBody>
      <Sender>string</Sender>
      <Destination>string</Destination>
    </SendSMS>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendSMSResponse xmlns="http://tempuri.org/">
      <SendSMSResult>string</SendSMSResult>
    </SendSMSResponse>
  </soap:Body>
</soap:Envelope>

**SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。显示的占位符需要替换为实际值。

POST /gmgwebservice/service.asmx HTTP/1.1
Host: 212.35.66.180
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <SendSMS xmlns="http://tempuri.org/">
      <UserName>string</UserName>
      <Password>string</Password>
      <MessageBody>string</MessageBody>
      <Sender>string</Sender>
      <Destination>string</Destination>
    </SendSMS>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <SendSMSResponse xmlns="http://tempuri.org/">
      <SendSMSResult>string</SendSMSResult>
    </SendSMSResponse>
  </soap12:Body>
</soap12:Envelope>

最佳答案

要在 Java 中实现简单的 SOAP 客户端,您可以使用 SAAJ 框架(它随 JSE 1.6 及更高版本提供):

SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.

请参阅下面使用 SAAJ 进行 SOAP Web 服务调用的工作示例(运行它!)。它调用this web service .

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 = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
            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();

        String serverURI = "http://ws.cdyne.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("example", serverURI);

        /*
        Constructed SOAP Request Message:
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
            <SOAP-ENV:Header/>
            <SOAP-ENV:Body>
                <example:VerifyEmail>
                    <example:email>mutantninja@gmail.com</example:email>
                    <example:LicenseKey>123</example:LicenseKey>
                </example:VerifyEmail>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
         */

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
        soapBodyElem1.addTextNode("mutantninja@gmail.com");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
        soapBodyElem2.addTextNode("123");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

        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);
    }

}

(上面的代码取自并改编自 this page 。)

关于用于调用 SOAP - Web 服务的 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5092371/

相关文章:

java - 如何模拟存在但无法读取的路径

ios - URLConnection sendSynchronousRequest 问题?

javascript - 如何在 JavaScript 中从不同端口向资源写入 URL(无需主机名)

python - 属性(property)似乎没有以正确的方式运作

java - 用java反射实例化私有(private)内部类

java - mongodb 中 spring 数据的 json 响应中缺少 id

java - 谁负责对象转换?

Javafx 无法在 Debian8 Arm 设备 NanoPC T3 上运行

java - JSONObject 不维护顺序

python - 在 Ruby 或 Python 中,Class 的概念是否可以重写?