【应用场景】


WebAPI登录一次默认会保持20分钟会话,如果20分钟内会调用其他接口,会话时间会重新算;同一用户调用接口,建议只登录一次,后续接口调用后判断返回消息代码,如果是消息代码等于1可以尝试重新登录后继续调用接口;

如果每次调用接口前都登录一次,大量调用后服务器上会有大量的上下文实例得不到及时释放,从而可能导致服务器内存高涨。



【案例演示】

通过webapi保存客户,一次登录后多次调用保存接口。


【实现步骤】

<1>创建控制台 , 引用Kingdee.BOS.WebApi.Client和Newtonsoft.Json。 

<2>代码示例。

using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; namespace Zlf.WebApiDemo {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("保存开始..");             string url = "http://localhost/k3cloud/";             var httpInvoke = new WebApiHttpInvoke(url, "67468ad9fb64d4", "demo", "2969888_47aD5+sL6kH8WV8u6eSASbzLTs7aRoLL", "566381bd78d64d83b6a2f9be05ccd01e");             for (int i = 0; i < 10; i++)             {                 string billNo = string.Format("20241129-{0}", i + 1);                 httpInvoke.Save("BD_Customer", billNo, CreateJson(billNo));             }             Console.WriteLine("保存完成..");             Console.ReadLine();         }         private static string CreateJson(string billNo)         {             var data = new             {                 IsVerifyBaseDataField = true,                 Model = new                 {                     FCreateOrgId = new                     {                         FNumber = "100"                     },                     FNumber = billNo,                     FUseOrgId = new                     {                         FNumber = "100"                     },                     FName = billNo,                     FCOUNTRY = new                     {                         FNumber = "China"                     },                     FIsDefPayer = false,                     FIsGroup = false,                     FCustTypeId = new                     {                         FNumber = "KHLB001_SYS"                     },                     FTRADINGCURRID = new                     {                         FNumber = "PRE001"                     },                     FTRANSLEADTIME = 0                 }             };             return JsonConvert.SerializeObject(data);         }     } }

<3>WebApiHttpInvoke.cs代码。

using Kingdee.BOS.WebApi.Client; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zlf.WebApiDemo {     class WebApiHttpInvoke     {         private readonly string _dbId, _userName, _appId, _appSecret;         private readonly object _obj = new object();         private readonly K3CloudApiClient _client;         private bool _isLogin;         public WebApiHttpInvoke(string url, string dbId, string userName, string appId, string appSecret)         {             this._dbId = dbId;             this._userName = userName;             this._appId = appId;             this._appSecret = appSecret;             _client = new K3CloudApiClient(url, 10 * 60);//超时时间设为10分钟         }         //保存单据         public void Save(string formId, string billNo, string data, bool isRetry = false)         {             string saveResult = _client.Save(formId, data);             var responseStatus = JObject.Parse(saveResult)["Result"]["ResponseStatus"];             var isSuccess = responseStatus["IsSuccess"].Value<bool>();             if (!isSuccess)             {                 var msgCode = responseStatus["MsgCode"].Value<int>();                 if (msgCode == 1)//会话丢失,可能未登录或者过期了                 {                     if (isRetry)                     {                         return;                     }                     _isLogin = false;                     Login(); //重新登录                     Save(formId, billNo, data, true);                 }                 else                 {                     //暂存单据                     Console.WriteLine("保存失败,返回结果:{0}", saveResult);                 }             }             else             {                 Console.WriteLine("保存成功,billNo={0} ", billNo);             }         }         /// <summary>         /// 登录操作         /// </summary>         /// <returns></returns>         private bool Login()         {             lock (_obj)             {                 if (!_isLogin)                 {                     //使用第三方系统登录授权                     var loginResult = _client.LoginByAppSecret(this._dbId, this._userName, this._appId, this._appSecret, 2052);                     if (loginResult.Contains("response_error"))                     {                         //系统异常,包括账套id错误                         throw new Exception(loginResult);                     }                     var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();                     if (resultType == 1)                     {                         _isLogin = true;                         return true;                     }                     throw new Exception(loginResult);                 }             }             return true;         }     } }

【功能验证】

<1>运行代码。

上传图片

<2>启用WebAPI日志 , 可以看到只调用一次登录。

上传图片

上一篇:WebAPI性能优化建议   下一篇:使用多用户多站点调用WebAPI接口简单示例

作者:cyoukon

来源:金蝶云社区

原文链接:https://vip.kingdee.com/knowledge/650363039845203200?specialId=650386937144032256&productLineId=1&isKnowledge=2&lang=zh-CN

著作权归作者所有。未经允许禁止转载,如需转载请联系作者获得授权。


标签: none

添加新评论