我在字符串中有以下消息,
"<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
<soapenv:Header/>
<soapenv:Body>
<def:XXX soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">someEncryptedTextGoesHere</in0>
</def:XXX>
</soapenv:Body>
</soapenv:Envelope>"
我试图在 SOAPMessage中转换它并访问它的正文以获取EncryptedText,但由于某种原因,包络变为空,我不明白 我试过了,
1)基本上我有HttpEntity,实体的主体是soaprequest
public String getEncryptedCodeFromSOAPRequest(HttpEntity<byte[]> requestEntity) throws SOAPException, IOException {
InputStream is = new ByteArrayInputStream(requestEntity.getBody());
log.info(requestEntity.getBody().toString());
String encryptedCode = "";
if (is.available() > 0) {
//SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
}
return encryptedCode;
}
2)尝试使用不同类型的SOAPConstants,如
SOAPMessage request = MessageFactory.newInstance(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE).createMessage(null,is);
URI_NS_SOAP_1_1_ENVELOPE使用这个,因为在soap msg中的xmlns:soapenv似乎是这样的,不知道这个登录是否正确。
如果您对此有任何建议,请告诉我
答案 0 :(得分:1)
尝试替换它:
if (is.available() > 0) {
//SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
}
有了这个:
if (is.available() > 0) {
SOAPMessage message = MessageFactory.newInstance("SOAP 1.2 Protocol").createMessage(null, is);
Document requestDocument = message.getSOAPBody().extractContentAsDocument();
NodeList nodes = requestDocument.getChildNodes();
//get parent node 1
Node parentNode = nodes.item(0);
//get child nodes, of parent node
NodeList childNodes = parentNode.getChildNodes();
//get first child node since theres only one in the example xml
Node childNode = childNodes.item(0);
//print type, value, etc
System.out.println(childNode.getNodeType() + " = " + childNode.getNodeName() + "/" + childNode.getNodeValue());
encryptedCode = childNode.getNodeValue();
}