如何在java编码中生成soap请求并获得响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22068864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to generate soap request and get response in java coding
提问by itsme
can anyone help me to develop a java program which will interact with the webservice.
谁能帮我开发一个与网络服务交互的java程序。
I created a simple webservice in netbeans. it generates wsdl file and i take the url from it.
我在 netbeans 中创建了一个简单的 web 服务。它生成 wsdl 文件,我从中获取 url。
By using the wsdl file that is created in netbeans, i have to send soap request and get response in a java program.
通过使用在netbeans 中创建的wsdl 文件,我必须发送soap 请求并在java 程序中获得响应。
I have the following piece of coding, but i have no idea on how to implement for my requirement
我有以下一段代码,但我不知道如何实现我的要求
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;
}
}
回答by Rudi Angela
I recommend using JAX-WS instead of handcrafting your SOAP message. With JAX-WS you generate the code to create the SOAP/XML and to send/receive the message/response. All you're left to do is set the values for the content you need to pass. In this case that would be creating a VerifyEmail object, setting its two attributes and calling the send method of the generated web service client:
我建议使用 JAX-WS 而不是手工制作您的 SOAP 消息。使用 JAX-WS,您可以生成用于创建 SOAP/XML 和发送/接收消息/响应的代码。剩下要做的就是为需要传递的内容设置值。在这种情况下,将创建一个 VerifyEmail 对象,设置它的两个属性并调用生成的 Web 服务客户端的 send 方法:
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);
All the classes mentioned here would be generated for you by JAXB, which is used by JAX-WS. You can find more detailed info here.
这里提到的所有类都将由 JAXB 为您生成,JAX-WS 使用它。您可以在此处找到更多详细信息。