使用 java 向 WebService 发出 SOAP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19291283/
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
SOAP request to WebService with java
提问by Pievis
I'm a bit confused about how to make a request to a webservice via java.
我对如何通过 java 向网络服务发出请求感到有些困惑。
For now the only thing that I've understand is that webservices uses xml structured messages, but still I didn't quite understood how to structure my request.
目前我唯一了解的是 webservices 使用 xml 结构化消息,但我仍然不太明白如何构建我的请求。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getProductDetails xmlns="http://magazzino.example.com/ws">
<productId>827635</productId>
</getProductDetails>
</soap:Body>
</soap:Envelope>
Basically I've to send 2 parameters to the web service and in return I expect two other parameters.
基本上,我必须向 Web 服务发送 2 个参数,作为回报,我需要另外两个参数。
I guess there are some jars that can do most of the job, but I didn't find any online. Can someone please explain me the basis?
我想有一些罐子可以完成大部分工作,但我没有在网上找到任何罐子。有人可以解释我的依据吗?
采纳答案by acdcjunior
A SOAP request is an XML file consisting of the parameters you are sending to the server.
SOAP 请求是一个 XML 文件,由您发送到服务器的参数组成。
The SOAP response is equally an XML file, but now with everything the service wants to give you.
SOAP 响应同样是一个 XML 文件,但现在包含服务想要提供给您的所有内容。
Basically the WSDL is a XML file that explains the structure of those two XML.
基本上,WSDL 是一个 XML 文件,它解释了这两个 XML 的结构。
To implement simple SOAP clients in Java, you can use the SAAJ framework (it is shipped with JSE 1.6 and above):
要在 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.
SOAP with Attachments API for Java (SAAJ)主要用于直接处理发生在任何 Web 服务 API 的幕后的 SOAP 请求/响应消息。它允许开发人员直接发送和接收soap 消息,而不是使用JAX-WS。
See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.
请参阅下面的使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!)。它调用此 Web 服务。
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
/*
The example below requests from the Web Service at:
http://www.webservicex.net/uszip.asmx?op=GetInfoByCity
To call other WS, change the parameters below, which are:
- the SOAP Endpoint URL (that is, where the service is responding from)
- the SOAP Action
Also change the contents of the method createSoapEnvelope() in this class. It constructs
the inner part of the SOAP envelope that is actually sent.
*/
String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx";
String soapAction = "http://www.webserviceX.NET/GetInfoByCity";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";
String myNamespaceURI = "http://www.webserviceX.NET";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="http://www.webserviceX.NET">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<myNamespace:GetInfoByCity>
<myNamespace:USCity>New York</myNamespace:USCity>
</myNamespace:GetInfoByCity>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace);
soapBodyElem1.addTextNode("New York");
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
回答by lkamal
When the WSDL is available, it is just two steps you need to follow to invoke that web service.
当 WSDL 可用时,您只需执行两个步骤即可调用该 Web 服务。
Step 1: Generate the client side source from a WSDL2Java
tool
步骤 1:从WSDL2Java
工具生成客户端源
Step 2: Invoke the operation using:
第 2 步:使用以下命令调用操作:
YourService service = new YourServiceLocator();
Stub stub = service.getYourStub();
stub.operation();
If you look further, you will notice that the Stub
class is used to invoke the service deployed at the remote location as a web service. When invoking that, your client actually generates the SOAP request and communicates. Similarly the web service sends the response as a SOAP. With the help of a tool like Wireshark, you can view the SOAP messages exchanged.
如果进一步观察,您会注意到Stub
该类用于调用部署在远程位置的服务作为 Web 服务。调用它时,您的客户端实际上会生成 SOAP 请求并进行通信。同样,Web 服务将响应作为 SOAP 发送。借助 Wireshark 之类的工具,您可以查看交换的 SOAP 消息。
However since you have requested more explanation on the basics, I recommend you to refer hereand write a web service with it's client to learn it further.
但是,由于您要求对基础知识进行更多解释,因此我建议您参考此处并使用其客户端编写 Web 服务以进一步了解它。
回答by Red Boy
I have come across other similar question here. Both of above answers are perfect, but here trying to add additional information for someone looking for SOAP1.1, and not SOAP1.2.
我接触过其他类似的问题在这里。以上两个答案都很完美,但这里尝试为寻找SOAP1.1而不是SOAP1.2 的人添加其他信息。
Just change one line code provided by @acdcjunior, use SOAPMessageFactory1_1Impl
implementation, it will change namespace to xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/", which is SOAP1.1 implementation.
只需更改@acdcjunior 提供的一行代码,使用SOAPMessageFactory1_1Impl
实现,它将名称空间更改为xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/",这是SOAP1.1 实现。
Change callSoapWebService
method first line to following.
将callSoapWebService
方法第一行更改为以下。
SOAPMessage soapMessage = SOAPMessageFactory1_1Impl.newInstance().createMessage();
SOAPMessage soapMessage = SOAPMessageFactory1_1Impl.newInstance().createMessage();
I hope it will be helpful to others.
我希望它会对其他人有所帮助。