JAX-WS动态调用Web Service

1、调用方式介绍

   调用WebService的Java客户端一般采用以下三种方式:

  生成的stub、proxy(动态代理)以及dispatch(动态调用接口)。dispatch又分为payload和message两种方式。

  这里仅就dispatch的message方式进行介绍。那好了,现在让我们一起体验WebService动态调用的好处吧!

2、动态调用WebService

  废话不说,直接看代码,是非功过通过研究代码自见分晓!

   动态调用的详细代码如下:

  

package com.majun.xml;

   import java.util.Map;
   importjavax.xml.namespace.QName;
   importjavax.xml.soap.MessageFactory;
   importjavax.xml.soap.SOAPBody;
   importjavax.xml.soap.SOAPBodyElement;
   importjavax.xml.soap.SOAPElement;
   importjavax.xml.soap.SOAPMessage;
   importjavax.xml.ws.BindingProvider;
   importjavax.xml.ws.Dispatch;
   importjavax.xml.ws.Service;
   importjavax.xml.ws.WebServiceException;
   importjavax.xml.ws.soap.SOAPBinding;

   public classDLLClient
   {
    


    // 名字空间 
     publicstatic final String targetNamespace = "http://service.billInface.boss.gmt/";
     //服务名
     publicstatic final String serName = "BillWebServiceService";
     //端口名
     publicstatic final String pName = "BillWebServicePort";
     //服务地址
    public static final StringendpointAddress     = "http://localhost:8080/BillInface/BillWebServicePort?wsdl";
     //方法名
    public static final String OPER_NAME ="getInBillingLoginInfo";
    //参数名
    public static final String INPUT_NMAE = "arg0";
 
    public static void main(String[] args) throws Exception
     {
       
       QName serviceName = new QName(targetNamespace, serName);

       
       QName portName = new QName(targetNamespace,pName);

       
       javax.xml.ws.Service service = Service.create(serviceName);
       service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);

       
       Dispatch<SOAPMessage> dispatch =service.createDispatch(portName,
       SOAPMessage.class, Service.Mode.MESSAGE);

      
       BindingProvider bp = (BindingProvider)dispatch;
       Map<String, Object>rc = bp.getRequestContext();
       rc.put(BindingProvider.SOAPACTION_USE_PROPERTY,Boolean.TRUE);
       rc.put(BindingProvider.SOAPACTION_URI_PROPERTY,OPER_NAME);

       MessageFactory factory =((SOAPBinding)bp.getBinding()).getMessageFactory();

      
       SOAPMessage request = factory.createMessage();
       SOAPBody body = request.getSOAPBody();

       QName payloadName = new QName(targetNamespace,OPER_NAME, "ns1");

       SOAPBodyElement payload =body.addBodyElement(payloadName);

       

       SOAPElement message = payload.addChildElement(INPUT_NMAE);
        message.addTextNode("x");

       SOAPMessage reply = null;

       
       try
       {
          reply = dispatch.invoke(request);
       }
       catch (WebServiceException wse)
       {
         wse.printStackTrace();
       }
 

      SOAPBody soapBody = reply.getSOAPBody();
       SOAPBodyElement nextSoapBodyElement =(SOAPBodyElement)soapBody.getChildElements().next();              
       SOAPElement soapElement =(SOAPElement)nextSoapBodyElement.getChildElements().next();
  
       System.out.println("获取回应信息为:" +soapElement.getValue());  
     }

以上文章转自 http://blog.sina.com.cn/s/blog_6458bf440100lf82.html

特别感谢马儿作者的文章帮我解决了WebService远程调用的解决方法,特此感谢作者。

你可能感兴趣的:(JAX-WS动态调用Web Service)