gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:19:23 31 4
gpt4 key购买 nike

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

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

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

URI_NS_SOAP_1_1_ENVELOPE 使用这个是因为 soap 消息中的 xmlns:soapenv 似乎是这样的,不知道这个登录是否正确。

如果您对此有什么建议,请告诉我

enter image description here

enter image description here

enter image description here

最佳答案

尝试替换这个:

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

关于java - SOAPMessage 的字符串在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668200/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com