SOAPMessage的字符串在Java中不起作用

tyro:

我在字符串中有以下消息,

"<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,但是由于某种原因envelop获取null,我不知道我尝试了如何,

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)尝试使用不同类型的SOAPConstant,例如

SOAPMessage request = MessageFactory.newInstance(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE).createMessage(null,is);

URI_NS_SOAP_1_1_ENVELOPE之所以使用它,是因为肥皂味精中的xmlns:soapenv似乎不知道此登录是否正确。

如果您对此有任何建议,请告诉我

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

JGlass:

尝试替换此:

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();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章