2014-02-27 49 views
1

任何人都可以帮助我开发一个java程序,它将与web服务交互。如何在java编码中生成soap请求并获得响应

我在netbeans中创建了一个简单的web服务。它会生成wsdl文件,并从中获取网址。

通过使用NetBeans中创建的wsdl文件,我必须发送肥皂请求并获得Java程序中的响应。

我有下面这段代码,但我对如何实现我的要求

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>[email protected]</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("[email protected]"); 
     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; 
    } 
} 

回答

4

我建议使用JAX-WS,而不是手工制作的SOAP消息不知道。使用JAX-WS生成代码来创建SOAP/XML并发送/接收消息/响应。你要做的就是设置你需要传递的内容的值。在这种情况下会创建一个VerifyEmail对象,设置它的两个属性和调用生成的Web服务客户端的发送方法:

ObjectFactory factory = new ObjectFactory; 
VerifyEmailRequest request = objectFactory.createVerifyEmailRequest(); 
VerifyEmail msg = objectFactory.createVerifyEmail(); 
msg.setEmail("[email protected]"); 
msg.setLicenseKey("myKey"); 
request.setVerifyEmail(msg); 
VerifyEmailResponse response = myClient.verifyEmail(reques); 

所有这里提到的类会为您JAXB生成,用于由JAX-WS提供。你可以找到更多的详细信息here

+0

如果可以,这很好,但情况并非总是如此。我的客户希望我重新发送记录的肥皂消息到终端。找出哪个确切的方法来调用哪个服务是一件痛苦的事情,并且重新发送SOAPEnvelope更加实用。 – Dormouse

+0

我没有看到任何问题。如果您在任何时候需要消息的XML表示,则可以使用JAXB Marshaller将消息序列化为XML。 –

+0

是的。然而,事实证明,只需打开端点的套接字,设置所需的HTTP标头,并将标头和SOAP信封流式传输到端点就可​​以了。 – Dormouse

相关问题