簡體   English   中英

如何在Java編碼中生成soap請求並獲得響應

[英]how to generate soap request and get response in java coding

誰能幫助我開發將與Web服務交互的Java程序。

我在netbeans中創建了一個簡單的Web服務。 它生成wsdl文件,我從中獲取網址。

通過使用在netbeans中創建的wsdl文件,我必須在Java程序中發送soap請求並獲得響應。

我有以下一段代碼,但是我不知道如何實現我的要求

import javax.xml.soap.*;

public class SOAPClientSAAJ2 {
    public static void main(String args[]) throws Exception {
        // 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);

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

        soapConnection.close();
    }

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

我建議使用JAX-WS而不是手工編寫SOAP消息。 使用JAX-WS,您可以生成代碼來創建SOAP / XML並發送/接收消息/響應。 您要做的就是為您需要傳遞的內容設置值。 在這種情況下,將創建一個VerifyEmail對象,設置其兩個屬性並調用生成的Web服務客戶端的send方法:

ObjectFactory factory = new ObjectFactory;
VerifyEmailRequest request = objectFactory.createVerifyEmailRequest();
VerifyEmail msg = objectFactory.createVerifyEmail();
msg.setEmail("myemail@provider.com");
msg.setLicenseKey("myKey");
request.setVerifyEmail(msg);
VerifyEmailResponse response = myClient.verifyEmail(reques);

這里提到的所有類都將由JAXB為您生成,而JAX-WS使用了JAXB。 您可以在此處找到更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM