SOAP消息的传递
【摘要】 上一篇说了SOAP消息的创建,那么创建好了的SOAP消息要怎么发送给服务端呢?
public class SoapTest { private String wsdlUri = "http://localhost:9999/ns?wsdl"; private String ns = "http://lenve.server/"; @Test public void t...
上一篇说了SOAP消息的创建,那么创建好了的SOAP消息要怎么发送给服务端呢?
public class SoapTest { private String wsdlUri = "http://localhost:9999/ns?wsdl"; private String ns = "http://lenve.server/"; @Test public void test3() { try { // 1.创建服务Service URL url = new URL(wsdlUri); QName sname = new QName(ns, "MyServerImplService"); Service service = Service.create(url, sname); // 2.创建Dispatch Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "MyServerImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); //3.创建SOAPMessage SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPBody body = msg.getSOAPPart().getEnvelope().getBody(); //4.创建QName来指定消息中传递的数据 QName ename = new QName(ns,"add","ns"); SOAPBodyElement ele = body.addBodyElement(ename); ele.addChildElement("a").setValue("3"); ele.addChildElement("b").setValue("6"); //5.通过Dispatch传递消息,同时收到响应消息 SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); String str = doc.getElementsByTagName("addResult").item(0).getTextContent(); System.out.println(); System.out.println(str); } catch (SOAPException | IOException e) { e.printStackTrace(); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
客户端输出:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><S:Body><ns2:addResponse xmlns:ns2="http://lenve.server/"><addResult>9</addResult></ns2:addResponse></S:Body></S:Envelope>
9
- 1
- 2
- 3
成功调用了服务端程序。代码中先定义了两个变量,第一个是地址,这个不用多解释,第二个是命名空间,这是从地址所表示的页面中得到的。,在创建dispatch是还用到了MyServerImplPort,这个也是从文档中获得,在文档的结尾。
。
文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。
原文链接:wangsong.blog.csdn.net/article/details/45644059
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)