我必须创建一个 Java 客户端来执行 Web 服务。我有一个包含整个 SOAP 请求(信封、 header 、正文)的 XML。
如何通过传递包含soap请求的xml文件来编写java代码来执行web服务?
我尝试了很多搜索,但找不到执行此操作的示例
服务器上的 Web 服务采用 SOAP 1.1,内容类型为“text/xml”
例如 wsdlLocation="http://localhost:8080/helloservice/hello?wsdl"
网络服务没有输入参数,这就是为什么数据必须作为 SOAP 请求完全传递。传递的数据是xml形式的。
示例 SOAP 请求 xml 文件示例 (Sample.xml)
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<putTxlife1203Info xmlns="http://www.openuri.org/">
<TXLife>
</TXLife>
</putTxlife1203Info>
</env:Body>
</env:Envelope>
如果有人能提供样本那就太好了
最佳答案
import javax.xml.soap.*;
public String callTestService(String soapRequestXml, String url) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapRequest = MessageFactory.newInstance().createMessage(new MimeHeaders(),
new ByteArrayInputStream(soapRequestXml.getBytes()));
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(soapRequest, url);
ByteArrayOutputStream soapResponseBaos = new ByteArrayOutputStream();
soapResponse.writeTo(soapResponseBaos);
String soapResponseXml = soapResponseBaos.toString();
return soapResponseXml;
}
关于java - Web 服务客户端调用 Web 服务客户端 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24740758/