1. 代码示例。
Java代码
- 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>
- *
- * 创建日期:2013-11-26
- *
- */
- 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方式访问接口
- *
- * paramVO
- * 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. 参考