zoukankan      html  css  js  c++  java
  • Java测试调用.net 接口服务

    package com.karros.test;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.xml.namespace.QName;
    
    import org.apache.axis.client.Call;
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    public class TestWsdl {
    
        protected final Log logger = LogFactory.getLog(getClass());
        public static void main(String[] args) throws Exception {   
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("syncTime", "20200401");
            Map<String, Object> resultMap = new HashMap<String, Object>();
            TestWsdl aa = new TestWsdl();
            resultMap= aa.getWebService(paramMap);
            System.out.println(resultMap);
        } 
    
    //    @Value("${weixin_sign_url}")
    //    private String endpoint;
        //  #微信签到云平台接口地址
        // weixin_sign_url=http://10.182.5.173:1111/Service/WXQDService.asmx
        
        public String endpoint = "http://10.182.5.96:102/Service/ORGService.asmx";
        public Map<String, Object> getWebService(Map<String, Object> paramMap) throws Exception {
            logger.debug("获取接口开始...");
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("success", true);
            try {
                String syncTime = (String) paramMap.get("syncTime");
                if (StringUtils.isBlank(syncTime)) {    //20200401
                    resultMap.put("success", false);
                    return resultMap;
                } else {
                    // 创建一个服务(service)调用(call)
                    org.apache.axis.client.Service service = new org.apache.axis.client.Service();
                    Call call = (Call) service.createCall();// 通过service创建call对象
                    // 设置service所在URL
                    call.setTargetEndpointAddress(new java.net.URL(endpoint));
    
                    call.setOperationName(new QName("http://tempuri.org/", "GetUserList"));
                    // .net 那边的方法 "http://tempuri.org/"这个也要注意Namespace的地址,不带也会报错
                    call.addParameter(new QName("http://tempuri.org/", "syncTime"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
    
                    call.setUseSOAPAction(true);
                    call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); // 返回参数的类型
                    call.setSOAPActionURI("http://tempuri.org/GetUserList"); // 这个也要注意
                    String userVo = (String) call.invoke(new Object[] { syncTime });
                    if (StringUtils.isBlank(userVo) || StringUtils.equals("[]", userVo)) {
                        resultMap.put("success", false);
                    } else {
                        resultMap.put("userVo", userVo);
                    }
                }
            } catch (Exception e) {
                resultMap.put("success", false);
                logger.error("获取信息异常", e);
            }
            logger.debug("获取接口结束...");
            return resultMap;
        }
    
    }

    基础:https://www.cnblogs.com/herizai/p/6957611.html

    package com.karros.test;
    import java.util.HashMap;import java.util.Map;
    import javax.xml.namespace.QName;
    import org.apache.axis.client.Call;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
    public class TestWsdl {
    protected final Log logger = LogFactory.getLog(getClass());public static void main(String[] args) throws Exception {   Map<String, Object> paramMap = new HashMap<String, Object>();paramMap.put("syncTime", "20200401");Map<String, Object> resultMap = new HashMap<String, Object>();TestWsdl aa = new TestWsdl();resultMap= aa.getWebService(paramMap);System.out.println(resultMap);} 
    //@Value("${weixin_sign_url}")//private String endpoint;//  #微信签到云平台接口地址// weixin_sign_url=http://10.182.5.173:1111/Service/WXQDService.asmxpublic String endpoint = "http://10.182.5.96:102/Service/ORGService.asmx";public Map<String, Object> getWebService(Map<String, Object> paramMap) throws Exception {logger.debug("获取接口开始...");Map<String, Object> resultMap = new HashMap<String, Object>();resultMap.put("success", true);try {String syncTime = (String) paramMap.get("syncTime");if (StringUtils.isBlank(syncTime)) {//20200401resultMap.put("success", false);return resultMap;} else {// 创建一个服务(service)调用(call)org.apache.axis.client.Service service = new org.apache.axis.client.Service();Call call = (Call) service.createCall();// 通过service创建call对象// 设置service所在URLcall.setTargetEndpointAddress(new java.net.URL(endpoint));
    call.setOperationName(new QName("http://tempuri.org/", "GetUserList"));// .net 那边的方法 "http://tempuri.org/"这个也要注意Namespace的地址,不带也会报错call.addParameter(new QName("http://tempuri.org/", "syncTime"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
    call.setUseSOAPAction(true);call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); // 返回参数的类型call.setSOAPActionURI("http://tempuri.org/GetUserList"); // 这个也要注意String userVo = (String) call.invoke(new Object[] { syncTime });if (StringUtils.isBlank(userVo) || StringUtils.equals("[]", userVo)) {resultMap.put("success", false);} else {resultMap.put("userVo", userVo);}}} catch (Exception e) {resultMap.put("success", false);logger.error("获取信息异常", e);}logger.debug("获取接口结束...");return resultMap;}
    }

  • 相关阅读:
    策略模式
    简单工厂模式
    单例模式
    sp_xml_preparedocument _使用 处理XML文档
    LINQ to XML
    动态Linq(结合反射)
    IEqualityComparer<T>接口
    Linq to object 技巧、用法集锦
    IComparer<T> 接口Linq比较接口
    Linq to BBJECT之非延时标准查询操作符
  • 原文地址:https://www.cnblogs.com/tldxh/p/12612047.html
Copyright © 2011-2022 走看看