第三方系统如何获取 星空PLM的电子仓访问凭证Token

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-23浏览:1

第三方系统如何获取 星空PLM的电子仓访问凭证Token

【应用场景】

第三方系统通过接口调用的方式来获取PLM电子仓的访问凭证Token


【注意事项】

此接口在PT-146934(包含)及之后可调用


【实现步骤】

<1>代码如下。 

    public class PLMWarehoustToken
    {

        private static PLMWarehoustToken _instance;

        public static PLMWarehoustToken Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PLMWarehoustToken();
                }
                return _instance;
            }
        }

        private PLMWarehoustToken()
        { }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="cloudServerUrl">Cloud服务器地址</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public string GetWareHouseToken(string cloudServerUrl, long userId)
        {
            object[] args = new object[] { userId };//UserID,从数据库中提取
            string token = PLMWarehoustToken.Instance.Execute(cloudServerUrl, "Kingdee.K3.PLM.ServiceFacade.ServicesStub.WarehouseBusinessService", "GetRegisterToken", args);
            return token;
        }

        private string Execute(string serverUrl, string className, string method, object[] args = null)
        {
            List<object> objs = new List<object>();
            if (args != null)
                objs = new List<object>(args);
            string content = JsonConvert.SerializeObject(objs);
            return Call(serverUrl, string.Format("{0}.{1}.common.kdsvc", className, method), content);
        }

        protected string Call(string serverUrl, string serviceName, string requestData)
        {
            string _wbsUrl = CombineUrl(serverUrl, serviceName);
            var request = GetRequest(_wbsUrl);
            JObject jObj = new JObject();
            jObj.Add("format", 1);
            jObj.Add("useragent", "ApiClient");
            jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());
            jObj.Add("parameters", requestData);
            jObj.Add("timestamp", DateTime.Now);
            jObj.Add("v", "1.0");
            string sContent = jObj.ToString();
            byte[] data = UnicodeEncoding.UTF8.GetBytes(sContent);
            string result = string.Empty;

            request.ContentLength = data.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
            }

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {

                response = (HttpWebResponse)ex.Response;
                if (response != null)
                {
                    throw new Exception(ReadResponse(response));
                }
                else throw ex;
            }
            result = ReadResponse(response);
            return result;
        }


        private HttpWebRequest GetRequest(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Post;
            request.Timeout = 120000;
            request.ReadWriteTimeout = 3000000;
            request.ContentType = "application/json";
            request.CookieContainer = GetCookieContainer(url);
            return request;
        }

        private List<Cookie> _cookies = new List<Cookie>();
        private CookieContainer GetCookieContainer(string serverUrl)
        {
            CookieContainer container = new CookieContainer();
            Uri uri = new Uri(serverUrl);
            foreach (Cookie cookie in _cookies)
            {
                container.Add(uri, cookie);
            }
            return container;
        }

        private string ReadResponse(HttpWebResponse response)
        {
            string result;
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
            }
            return result;
        }
        private string CombineUrl(string url, string path)
        {
            url = url.TrimEnd('/', '\\');
            path = path.TrimStart('/', '\\');
            return url + Path.AltDirectorySeparatorChar + path;
        }
    }


<2>调用方式参考:

string cloudServerUrl = "http://192.168.0.1/K3Cloud/";

long userId = 140155;//用户的userId,

PLMWarehoustToken.Instance.GetWareHouseToken(cloudServerUrl, userId );

第三方系统如何获取 星空PLM的电子仓访问凭证Token

【应用场景】第三方系统通过接口调用的方式来获取PLM电子仓的访问凭证Token【注意事项】此接口在PT-146934(包含)及之后可调用【实现步骤...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息