1. 程式人生 > 實用技巧 >C#對接金蝶介面(K3)

C#對接金蝶介面(K3)

金蝶查詢介面:

/// <summary>
        /// 呼叫金蝶的查詢介面
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            // 使用webapi引用元件Kingdee.BOS.WebApi.Client.dll
            K3CloudApiClient client = new
K3CloudApiClient("http://8.136.98.54/k3cloud/"); var loginResult = client.ValidateLogin("5fe007ae85d7d5", "Administrator", "888888", 2052); var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>(); //登入結果型別等於1,代表登入成功 if (resultType == 1
) { //string st = "{\n\"FormId\":\"BD_Customer\",\"FieldKeys\":\"FNumber,FName\",\"FilterString\":\"\"\n}"; var req = context.Request.Form["params"]; var test = client.ExecuteBillQuery(req); context.Response.Write(JsonConvert.SerializeObject(test)); } }
api.ashx

呼叫金蝶查詢介面(傳參和取資料的作用):

private dynamic GetWorkOrder(string no)
        {
static string KDApiUrl = "http://vps2.seekersoft.cn:8037/api.ashx";
            var client = new RestClient(KDApiUrl);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AlwaysMultipartFormData = true;
            request.AddParameter("params", "{\"FormId\":\"PRD_MO\",\"FieldKeys\":\"FMaterialId.FNumber,F_ora_Text,FSpecification,FLot.FNumber,FDate,FBillNo\",\"FilterString\":\"FBillNo  = '" + no + "'\"}");
            IRestResponse response = client.Execute(request);
            var data = JsonConvert.DeserializeObject<List<List<string>>>(response.Content);
            List<dynamic> result = new List<dynamic>(data.Count);
            foreach (var li in data)
            {
                return (new
                {
                    FMaterimalNo = li[0],// 物料編碼
                    FSaleOrderNo = li[1], //銷售訂單號
                    FSpecification=li[2],//大昌規格型號
                    FlotFNumber=li[3],//批號
                    FDate=li[4],//生產日期
                    FBillNo=li[5]//生產工單單據號
                });
            }
            return null;
        }
KDApi