我有以下代码:
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
MimeHeaders mimeHeader = sm.getMimeHeaders();
//change header's attribute
mimeHeader.setHeader("SOAPAction", "\"urn:Belkin:service:basicevent:1#GetBinaryState\"");
//sh.addAttribute(SOAPFactory.newInstance().createName("SOAPAction", "", "urn:Belkin:service:basicevent:1#SetBinaryState"),"");
QName bodyName = new QName("urn:Belkin:service:basicevent:1", "GetBinaryState", "u");
//sm.add
SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
QName qn = new QName("BinaryState");
System.out.println("\n Soap Request:\n");
sm.writeTo(System.out);
System.out.println();
URL endpoint = new URL("http://" + ipAddress + ":49153/upnp/control/basicevent1");
SOAPMessage response = connection.call(sm, endpoint);
connection.close();
System.out.println(response.getContentDescription());
} catch (Exception ex) {
ex.printStackTrace();
}
给我这个回应(在wireshark中)
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetBinaryStateResponse xmlns:u="urn:Belkin:service:basicevent:1">
<BinaryState>0</BinaryState>
</u:GetBinaryStateResponse>
</s:Body>
</s:Envelope>
我想BinaryState
在 Java 中获取值 (0) 或 (1)。我很难弄清楚它在SOAPMessage
响应中的位置。帮助会很棒。或者即使我可以将 XML 响应放在一个字符串中并自己解析它。