先用一下比较基础的隐式方式处理我的SOAPHeader消息,注意的是QName的使用,代码如下:
1 public static void main(String[] args) { 2 try { 3 //创建服务 4 url = new URL("http://localhost:9999/com?wsdl"); //ns是namespace,即:命名空间
5 String ns = "http://www.xyw.com/wsdl"; 6 QName qname = new QName(ns,"MyServiceImplService"); 7 Service service = Service.create(url, qname); 8 // 创建服务代理 9 QName pname = new QName(ns,"MyServiceImplPort"); 10 Dispatch<SOAPMessage> dis = service.createDispatch(pname, SOAPMessage.class, Service.Mode.MESSAGE); 11 // 创建消息体 12 SOAPMessage msg = MessageFactory.newInstance().createMessage(); 13 SOAPEnvelope env = msg.getSOAPPart().getEnvelope(); 14 SOAPHeader header = env.getHeader(); 15 SOAPBody body = env.getBody(); 16 //对SOAPHeader进行操作 17 if(header == null) header = env.addHeader(); 18 QName hname = new QName(ns,"headerInfo"); 19 header.addHeaderElement(hname).setValue("MyHeaderInfo"); 20 //对SOAPBody处理 21 QName bname = new QName(ns,"add","ns"); 22 SOAPBodyElement sbe = body.addBodyElement(bname); 23 sbe.addChildElement("a").setValue("21"); 24 sbe.addChildElement("b").setValue("4"); 25 26 msg.writeTo(System.out); 27 System.out.println(" invoking..."); 28 //调用方法并返回SOAPMessage 29 SOAPMessage smresponse = dis.invoke(msg); 30 System.out.println(smresponse); 31 smresponse.writeTo(System.out); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 36 }