加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice_12_传递SOAP的消息和处理

发布时间:2020-12-17 00:11:57 所属栏目:安全 来源:网络整理
导读:非常感谢 孙浩 老师。 ? /** * @Title: test02 * @Description: 用SOAPMessage传递SOAP的消息和处理 * @param * @return void * @throws */@Testpublic void test02() {try {// 创建访问wsdl服务的URLURL url = new URL("http://localhost:9999/ns?wsdl");//

非常感谢孙浩老师。

?

/**
	 * @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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读