package com.yihu.jw.hospital.prescription.service.entrance.util;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;
import java.util.Map;


/**
 * Created by zdm on 2019/5/31.
 * webservice调用服务
 **/
public class WebserviceUtil {

    private static Logger logger = LoggerFactory.getLogger(WebserviceUtil.class);

   /*  * webservice 调用接口*/

    public static String post(String urlString,String namespace,String api, Map<String,String> params) throws Exception {
        try {
            Service service = new Service();
            // 通过service创建call对象
            Call call = (Call) service.createCall();
            // 设置service所在URL
            call.setTargetEndpointAddress(new java.net.URL(urlString));
            call.setOperationName(new QName(namespace, api));
            call.setUseSOAPAction(true);
            Object[] objs = null;
            if (params != null && params.size() > 0) {
                objs = new Object[params.size()];
                int i = 0;
                for (String key : params.keySet()) {
                    //接口的参数
                    call.addParameter(new QName(key), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
                    objs[i] = params.get(key);
                    i++;
                }
            }
            //设置返回类型
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            String ret = (String) call.invoke(objs);
            logger.info("=======>请求结果.toString()：" + ret.toString());
            return ret.toString();

        } catch (Exception e) {
            logger.info("=======>CA请求报错：" + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }


}
