由于另外系统用的是Microsoft SOAP Toolkit 工具发布的webservice。对应的调用方式也只能采用SOAP方式。
?
1. 下载jar包。 需要jar包:?soap.jar 和 mail-1.4.1.jar(已上传附件)
?
2. 代码示例。
?
import java.net.URL; import java.util.Vector; import nc.bs.logging.Logger; import nc.vo.levmpub.card.WebServiceConst; import nc.vo.levmpub.card.WebServiceParamVO; import nc.vo.pub.BusinessException; import org.apache.soap.Constants; import org.apache.soap.Fault; import org.apache.soap.encoding.SOAPMappingRegistry; import org.apache.soap.encoding.soapenc.StringDeserializer; import org.apache.soap.rpc.Call; import org.apache.soap.rpc.Parameter; import org.apache.soap.rpc.Response; import org.apache.soap.transport.http.SOAPHTTPConnection; import org.apache.soap.util.xml.QName; /** * * <b> 访问散装控制系统WebService(采用SOAP方式) </b> * <p> * 详细描述功能 * </p> * @since * 创建日期:2013-11-26 * @author */ public class WebServiceWrapper { public String callWebServiceMethod(WebServiceParamVO paramVO) throws BusinessException { String returnValue = null; try { returnValue = callMethod(paramVO); } catch (Exception e) { throw new BusinessException("调用散装控制系统接口出现错误: " + e.getMessage()); } return returnValue; } /** * 由于散装控制系统的wsdl是用Microsoft SOAP Toolkit自动生成的,只能通过soap方式访问接口 * * @param paramVO * @throws Exception */ private String callMethod(WebServiceParamVO paramVO) throws Exception { SOAPMappingRegistry smr = new SOAPMappingRegistry(); StringDeserializer sd = new StringDeserializer(); smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("", "Result"), null, null, sd); // 创建传输路径和参数 SOAPHTTPConnection st = new SOAPHTTPConnection(); // 创建调用 Call call = new Call(); call.setSOAPTransport(st); call.setSOAPMappingRegistry(smr); call.setTargetObjectURI(paramVO.getTargetObjectURI()); call.setMethodName(paramVO.getMethodName()); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); Vector<Parameter> params = getParameter(paramVO); call.setParams(params); URL url = new URL(paramVO.getWsdlUrl()); Response resp = call.invoke(url, paramVO.getSoapAction()); if (resp == null) { return null; } if (resp.generatedFault()) { Fault fault = resp.getFault(); Logger.error(fault.toString()); throw new BusinessException(fault.getFaultString()); } Parameter ret = resp.getReturnValue(); Object value = ret.getValue(); return (String)value; } private Vector<Parameter> getParameter(WebServiceParamVO paramVO) { Vector<Parameter> params = new Vector<Parameter>(); params.addElement(new Parameter(WebServiceConst.CUSERNAME, String.class, paramVO.getUserName(), null)); params.addElement(new Parameter(WebServiceConst.CPASSWORD, String.class, paramVO.getPassword(), null)); params.addElement(new Parameter(WebServiceConst.NTYPE, Integer.class, paramVO.getNtype(), null)); params.addElement(new Parameter(WebServiceConst.CCARNO, String.class, paramVO.getCarno(), null)); params.addElement(new Parameter(WebServiceConst.CCKBM, String.class, paramVO.getStorcode(), null)); params.addElement(new Parameter(WebServiceConst.CCHBM, String.class, paramVO.getInvcode(), null)); return params; } }
?
说明:
?
3. 参考。
?
10种语言的webservice的调用过程
?
java调用webservice接口方法
?
Web Services between .NET, Java and MS SOAP Toolkit