jax-ws 客户端动态访问小结

最近研究java6自带的webservice包(jax-ws),一些代码分享下。

public static void procWeb() throws Exception{
		QName serviceName = new QName("http://WebXml.com.cn/", "qqOnlineWebService");
		
		//QName for Port As defined in wsdl.
		QName portName = new QName("http://WebXml.com.cn/", "qqOnlineWebServiceSoap");
		
//		//Endpoint Address
		String  endpointAddress = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
		
		//Create a dynamic Service instance
		Service service = Service.create(serviceName);
		
		service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);  
		
		//Create a dispatch instance
		Dispatch<SOAPMessage> dispatch = 
		   service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
		
		// Use Dispatch as BindingProvider
		BindingProvider bp = (BindingProvider) dispatch;
		
		// Optionally Configure RequestContext to send SOAPAction HTTP Header
		Map<String, Object> rc = bp.getRequestContext();
		rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
		rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://WebXml.com.cn/qqCheckOnline");
		
		// Obtain a preconfigured SAAJ MessageFactory
		MessageFactory factory = ((SOAPBinding)bp.getBinding()).getMessageFactory();

		// Create SOAPMessage Request
		SOAPMessage request = factory.createMessage();

		// Request Body
		SOAPBody body = request.getSOAPBody();

		// Compose the soap:Body payload
		QName payloadName =
		   new QName("http://WebXml.com.cn/", "qqCheckOnline");

		SOAPBodyElement payload = body.addBodyElement(payloadName); 

		SOAPElement message = payload.addChildElement("qqCode");

		message.addTextNode("88888");

		// Invoke the endpoint synchronously
		SOAPMessage reply = null;

		try {
			//Invoke Endpoint Operation and read response
			reply = dispatch.invoke(request);
		} catch (WebServiceException wse){
			wse.printStackTrace();
		}

		// process the reply
		SOAPBody bodyRes = reply.getSOAPBody();
		
		SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement)bodyRes.getChildElements().next();              
        SOAPElement soapElement = (SOAPElement)nextSoapBodyElement.getChildElements().next();
		String strmsg = soapElement.getValue();

		System.out.println(strmsg);

	}
 

猜你喜欢

转载自voll588.iteye.com/blog/1162488