在 Java 中使用 SOAP Web 服务(具有安全性)
Consume SOAP web service (having security) in Java
我想 POST 将以下 XML 添加到 Web 服务 :-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<soapenv:Header>
<Security xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>xyzUser</Username>
<Password>xyzPass</Password>
</UsernameToken>
</Security>
</soapenv:Header>
<soapenv:Body>
<v1:OrderSearchRequest>
<v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber>
</v1:OrderSearchRequest>
</soapenv:Body>
</soapenv:Envelope>
我期待以下回复 XML :-
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<v1:OrderSearchResponse xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<v1:error>
<v1:errorCode>ERRODR01</v1:errorCode>
<v1:errorMessage>Order Number is Invalid</v1:errorMessage>
</v1:error>
</v1:OrderSearchResponse>
</soapenv:Body>
</soapenv:Envelope>
但是,我收到以下响应 XML,表明存在一些错误:-
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
</env:Header>
<env:Body>
<env:Fault>
<faultcode>env:Server
</faultcode>
<faultstring>
</faultstring>
<detail xmlns:fault="http://www.vordel.com/soapfaults" fault:type="faultDetails">
</detail>
</env:Fault>
</env:Body>
</env:Envelope>
我正在使用 Java 8. 我尝试在两个不同的程序中使用 Apache HTTPClient(版本 4.4.1)和 SAAJ 进行 POST 操作,但无法修复这个。有人可以帮忙吗?
SAAJ代码如下:-
public class RequestInitiation {
public static void main(String args[]) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "<service_endpoint>";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://<some_site>/retail/schema/inventory/orderlookupservice/v1";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("v1", serverURI);
SOAPHeader header = envelope.getHeader();
SOAPElement security =
header.addChildElement("Security", "", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
security.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "");
SOAPElement username =
usernameToken.addChildElement("Username", "");
username.addTextNode("xyzUser");
SOAPElement password =
usernameToken.addChildElement("Password", "");
password.addTextNode("xyzPass");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("OrderSearchRequest", "v1");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("RETAS400OrderNumber", "v1");
soapBodyElem1.addTextNode("1");
soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}
HTTPClient代码如下:-
public class PostSOAPRequest {
public static String post() throws Exception {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("<service_endpoint>");
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://<some_site>/retail/schema/inventory/orderlookupservice/v1\"><soapenv:Header><Security xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>xyzUser</Username><Password>xyzPass</Password></UsernameToken></Security></soapenv:Header><soapenv:Body><v1:OrderSearchRequest><v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber></v1:OrderSearchRequest></soapenv:Body></soapenv:Envelope>";
HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
return result;
}
public static void main(String[] args) {
try {
System.out.println(post());
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:站点名称、端点、用户名和密码在此消息中被替换为虚拟值。
看不到服务器端,我不知道如何提供帮助。
SOAP-ENV:Server There was a problem with the server, so the message
could not proceed.
不幸的是,您需要查看服务器日志以确定发生了什么。
我遇到了完全相同的问题,但是在另一个平台集成上。通过检查正在使用的 WSDL XML Web 服务的 SOAP 操作解决了这个问题,在我的代码中复制并粘贴正确的 SOAP 操作并且它起作用了。
此致。
发生了 2 个级别的操作:1. 应用凭据 2. 发送消息。此两步机制在 SAAJ 中可用。
我想 POST 将以下 XML 添加到 Web 服务 :-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<soapenv:Header>
<Security xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>xyzUser</Username>
<Password>xyzPass</Password>
</UsernameToken>
</Security>
</soapenv:Header>
<soapenv:Body>
<v1:OrderSearchRequest>
<v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber>
</v1:OrderSearchRequest>
</soapenv:Body>
</soapenv:Envelope>
我期待以下回复 XML :-
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<v1:OrderSearchResponse xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<v1:error>
<v1:errorCode>ERRODR01</v1:errorCode>
<v1:errorMessage>Order Number is Invalid</v1:errorMessage>
</v1:error>
</v1:OrderSearchResponse>
</soapenv:Body>
</soapenv:Envelope>
但是,我收到以下响应 XML,表明存在一些错误:-
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
</env:Header>
<env:Body>
<env:Fault>
<faultcode>env:Server
</faultcode>
<faultstring>
</faultstring>
<detail xmlns:fault="http://www.vordel.com/soapfaults" fault:type="faultDetails">
</detail>
</env:Fault>
</env:Body>
</env:Envelope>
我正在使用 Java 8. 我尝试在两个不同的程序中使用 Apache HTTPClient(版本 4.4.1)和 SAAJ 进行 POST 操作,但无法修复这个。有人可以帮忙吗?
SAAJ代码如下:-
public class RequestInitiation {
public static void main(String args[]) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "<service_endpoint>";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://<some_site>/retail/schema/inventory/orderlookupservice/v1";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("v1", serverURI);
SOAPHeader header = envelope.getHeader();
SOAPElement security =
header.addChildElement("Security", "", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
security.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "");
SOAPElement username =
usernameToken.addChildElement("Username", "");
username.addTextNode("xyzUser");
SOAPElement password =
usernameToken.addChildElement("Password", "");
password.addTextNode("xyzPass");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("OrderSearchRequest", "v1");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("RETAS400OrderNumber", "v1");
soapBodyElem1.addTextNode("1");
soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}
HTTPClient代码如下:-
public class PostSOAPRequest {
public static String post() throws Exception {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("<service_endpoint>");
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://<some_site>/retail/schema/inventory/orderlookupservice/v1\"><soapenv:Header><Security xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>xyzUser</Username><Password>xyzPass</Password></UsernameToken></Security></soapenv:Header><soapenv:Body><v1:OrderSearchRequest><v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber></v1:OrderSearchRequest></soapenv:Body></soapenv:Envelope>";
HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
return result;
}
public static void main(String[] args) {
try {
System.out.println(post());
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:站点名称、端点、用户名和密码在此消息中被替换为虚拟值。
看不到服务器端,我不知道如何提供帮助。
SOAP-ENV:Server There was a problem with the server, so the message could not proceed.
不幸的是,您需要查看服务器日志以确定发生了什么。
我遇到了完全相同的问题,但是在另一个平台集成上。通过检查正在使用的 WSDL XML Web 服务的 SOAP 操作解决了这个问题,在我的代码中复制并粘贴正确的 SOAP 操作并且它起作用了。
此致。
发生了 2 个级别的操作:1. 应用凭据 2. 发送消息。此两步机制在 SAAJ 中可用。