非常感谢孙浩老师。
/** * @Title: test02 * @Description: 用SOAPMessage传递SOAP的消息和处理 * @param * @return void * @throws */ @Test public void test02() { try { // 创建访问wsdl服务的URL URL url = new URL("http://localhost:9999/ns?wsdl"); // 通过Qname指明服务的具体信息 QName name = new QName("http://soap.lichen.cn/","MyServiceImplService"); // 创建service Service service = Service.create(url,name); // 创建dispatch dispatch<SOAPMessage> dispatch = service.createdispatch(new QName( "http://soap.lichen.cn/","MyServiceImplPort"),SOAPMessage.class,Service.Mode.MESSAGE); //创建SOAPmessage SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); QName qname = new QName("http://soap.lichen.cn/","add","xsd"); SOAPBodyElement bodyElement = body.addBodyElement(qname); bodyElement.addChildElement("a").setValue("50"); bodyElement.addChildElement("b").setValue("34"); //输入创建SOAPmessage message.writeto(System.out); System.out.println("\n"+"-----------invoking-------------"); //传递消息并且得到结果 SOAPMessage responseMessage = dispatch.invoke(message); //输出得到的SOAPmessage responseMessage.writeto(System.out); //将响应的消息转换为dom对象 Document doc = responseMessage.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); String str = doc.getElementsByTagName("addResult").item(0).getTextContent(); System.out.println("\n"+str); } catch (SOAPException e) { // Todo Auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo Auto-generated catch block e.printstacktrace(); } }
得到结果:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><xsd:add xmlns:xsd="http://soap.lichen.cn/"><a>50</a><b>34</b></xsd:add></SOAP-ENV:Body></SOAP-ENV:Envelope> -----------invoking------------- <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body><ns2:addResponse xmlns:ns2="http://soap.lichen.cn/"><addResult>84</addResult></ns2:addResponse></S:Body></S:Envelope> 84
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。