java调用.net webservice的完整实例
Java客户端调用Web Service的一般有以下三种方式:
生成的stub、proxy(动态代理)以及dispatch(动态调用接口)。dispatch又分为payload和message两种方式。
这个实例仅就dispatch的message方式进行介绍。
C#代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Services.Description; namespace WebService1 { /// <summary> /// Service1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)] public class Service1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string strHost) { return "Hello World, "+strHost; } } }
Java调用程序
import java.util.Map; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPMessage; import javax.xml.ws.BindingProvider; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import javax.xml.ws.soap.SOAPBinding; public class WebServiceTest { /** * @param args */ public static final String targetNamespace = "http://tempuri.org/"; //服务名 public static final String serName = "Service1"; //端口名 public static final String pName = "5756"; //服务地址 public static final String endpointAddress = "http://localhost:5756/Service1.asmx?wsdl"; //方法名 public static final String OPER_NAME = "HelloWorld"; //参数名 public static final String INPUT_NMAE = "strHost"; 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); SOAPBodyElement payload = body.addBodyElement(payloadName); SOAPElement message = payload.addChildElement(INPUT_NMAE); message.addTextNode("xyz"); 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()); } }
输出:
Hello World, xyz