K/3 Cloud Web API集成开发客户端不引用组件示例【分享】
金蝶云社区-天冥异
天冥异
24人赞赏了该文章 6035次浏览 未经作者许可,禁止转载编辑于2018年07月19日 20:28:23

先上张Hello World成功返回的图片:
WebAPIWN.png 

本帖子达到目的:一箭三雕
1、不引用“Kingdee.BOS.WebApi.Client.dll”,进行K/3 Cloud Web API开发
2、示例中提供的是万能接口调用
3、对于其他标准接口调用已顺带实现

本帖子主要针对Web API客户端调用,旨在提供不引用“Kingdee.BOS.WebApi.Client.dll”组件既可以运行的Web API接口示例代码。
之所以会有本帖子码,主要出于2点原因考虑
1、是“Kingdee.BOS.WebApi.Client.dll”客户端需引用组件,此组件只提供了Framework4.0版本,对于非Framework4.0环境开发,会带来问题。
2、“Kingdee.BOS.WebApi.Client.dll”组件本身是用.Net C#语言所编写的组件,其他非.Net系语言进行开发引用一定是用不了的,对于其他支持http协议语言,参考本帖子完全可以自定义封装实现。

提供的实例代码
1、只对.Net Framework组件有引用,其中对第三方组件“Newtonsoft.Json.dll”引用,只是对Json数据进行解析需求,完全可以替换为你所熟悉的Json解析组件。
2、是完全可以直接运行的代码。
按照本帖子设置,一定可以返回到客户端“Hello World”。
二步走
第一步:编写服务端个性需求代码(标准的K/3 Cloud组件开发环境,编译组件,置于网站 bin目录)
第二步:编写客户端调用代码(此时你的代码完全可以用,任何支持http的语言去编写)


C# 服务端个性需求自定义代码:

  1. /*

  2. * 服务需要引用组件如下

  3. * Kingdee.BOS.dll、Kingdee.BOS.ServiceFacade.KDServiceFx.dll、Kingdee.BOS.WebApi.ServicesStub.dll

  4. */

  5. using System;

  6. using System.Collections.Generic;

  7. using System.Linq;

  8. using System.Text;

  9. using Kingdee.BOS.ServiceFacade.KDServiceFx;

  10. using Kingdee.BOS.WebApi.ServicesStub;

  11. using Kingdee.BOS.JSON;



  12. namespace Kingdee.BOS.WebAPI.ServiceExtend.ServicesStub

  13. {

  14.     public class CustomBusinessService : AbstractWebApiBusinessService

  15.     {

  16.         public CustomBusinessService(KDServiceContext context)

  17.             : base(context)

  18.         { }


  19.         public JSONArray ExecuteService(string parameter)

  20.         {

  21.             JSONArray jsonArray = new JSONArray();

  22.             jsonArray.Add("Hello World");

  23.             return jsonArray;

  24.         }

  25.     }

  26. }

复制代码



如下是客户端代码:是一个轻量级的工程,建立.Net C# 控制台程序

http请求封装:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.IO;

  4. using System.Linq;

  5. using System.Net;

  6. using System.Text;

  7. using Newtonsoft.Json.Linq;

  8. using Newtonsoft.Json;


  9. namespace ConsoleApplication.WebAPI

  10. {

  11.     class HttpClient

  12.     {

  13.         /// <summary>

  14.         /// Seivice URL

  15.         /// </summary>

  16.         public string Url { get; set; }

  17.         /// <summary>

  18.         /// 内容

  19.         /// </summary>

  20.         public string Content { get; set; }

  21.         /// <summary>

  22.         /// Cookie,保证登录后,所有访问持有一个Cookie;

  23.         /// </summary>

  24.         static CookieContainer Cookie = new CookieContainer();


  25.         /// <summary>

  26.         /// HTTP访问

  27.         /// </summary>

  28.         public string SysncRequest()

  29.         {

  30.             HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;

  31.             httpRequest.Method = "POST";

  32.             httpRequest.ContentType = "application/json";

  33.             httpRequest.CookieContainer = Cookie;

  34.             httpRequest.Timeout = 1000 * 60 * 10;//10min


  35.             using (Stream reqStream = httpRequest.GetRequestStream())

  36.             {

  37.                 JObject jObj = new JObject();

  38.                 jObj.Add("format", 1);

  39.                 jObj.Add("useragent", "ApiClient");

  40.                 jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());

  41.                 jObj.Add("parameters", Content);

  42.                 jObj.Add("timestamp", DateTime.Now);

  43.                 jObj.Add("v", "1.0");

  44.                 string sContent = jObj.ToString();

  45.                 var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);

  46.                 reqStream.Write(bytes, 0, bytes.Length);

  47.                 reqStream.Flush();

  48.             }

  49.             using (var repStream = httpRequest.GetResponse().GetResponseStream())

  50.             {

  51.                 using (var reader = new StreamReader(repStream))

  52.                 {

  53.                     return ValidateResult(reader.ReadToEnd());

  54.                 }

  55.             }

  56.         }


  57.         private static string ValidateResult(string responseText)

  58.         {

  59.             if (responseText.StartsWith("response_error:"))

  60.             {

  61.                 var failText = responseText.TrimStart("response_error:".ToCharArray());

  62.             }

  63.             return responseText;

  64.         }

  65.     }

  66. }


复制代码


操作辅助类封装:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Newtonsoft.Json;


  6. namespace ConsoleApplication.WebAPI

  7. {

  8.     public static class InvokeHelper

  9.     {

  10.         private static string CloudUrl = "http://192.168.66.60/k3cloud/";//K/3 Cloud 业务站点地址


  11.         /// <summary>

  12.         /// 登陆

  13.         /// </summary>

  14.         public static string Login()

  15.         {

  16.             HttpClient httpClient = new HttpClient();

  17.             httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");


  18.             List<object> Parameters = new List<object>();

  19.             Parameters.Add("558cbb01bfc79b");//帐套Id

  20.             Parameters.Add("Administrator");//用户名

  21.             Parameters.Add("888888");//密码


  22.             Parameters.Add(2052);

  23.             httpClient.Content = JsonConvert.SerializeObject(Parameters);

  24.             return httpClient.SysncRequest();

  25.         }


  26.         /// <summary>

  27.         /// 保存

  28.         /// </summary>

  29.         /// <param name="formId"></param>

  30.         /// <param name="content"></param>

  31.         /// <returns></returns>

  32.         public static string Save(string formId, string content)

  33.         {

  34.             HttpClient httpClient = new HttpClient();

  35.             httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");


  36.             List<object> Parameters = new List<object>();

  37.             //业务对象Id 

  38.             Parameters.Add(formId);

  39.             //Json字串

  40.             Parameters.Add(content);

  41.             httpClient.Content = JsonConvert.SerializeObject(Parameters);

  42.             return httpClient.SysncRequest();

  43.         }


  44.         /// <summary>

  45.         /// 删除

  46.         /// </summary>

  47.         /// <param name="formId"></param>

  48.         /// <param name="content"></param>

  49.         /// <returns></returns>

  50.         public static string Delete(string formId, string content)

  51.         {

  52.             HttpClient httpClient = new HttpClient();

  53.             httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc");


  54.             List<object> Parameters = new List<object>();

  55.             //业务对象Id 

  56.             Parameters.Add(formId);

  57.             //Json字串

  58.             Parameters.Add(content);

  59.             httpClient.Content = JsonConvert.SerializeObject(Parameters);

  60.             return httpClient.SysncRequest();

  61.         }


  62.         /// <summary>

  63.         /// 审核

  64.         /// </summary>

  65.         /// <param name="formId"></param>

  66.         /// <param name="content"></param>

  67.         /// <returns></returns>

  68.         public static string Audit(string formId, string content)

  69.         {

  70.             HttpClient httpClient = new HttpClient();

  71.             httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");


  72.             List<object> Parameters = new List<object>();

  73.             //业务对象Id 

  74.             Parameters.Add(formId);

  75.             //Json字串

  76.             Parameters.Add(content);

  77.             httpClient.Content = JsonConvert.SerializeObject(Parameters);

  78.             return httpClient.SysncRequest();

  79.         }


  80.         /// <summary>

  81.         /// 自定义

  82.         /// </summary>

  83.         /// <param name="key">自定义方法标识</param>

  84.         /// <param name="args">参数</param>

  85.         /// <returns></returns>

  86.         public static string AbstractWebApiBusinessService(string key, List<object> args)

  87.         {

  88.             HttpClient httpClient = new HttpClient();

  89.             httpClient.Url = string.Concat(CloudUrl, key, ".common.kdsvc");


  90.             httpClient.Content = JsonConvert.SerializeObject(args);

  91.             return httpClient.SysncRequest();

  92.         }

  93.     }

  94. }


复制代码


调用代码:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Newtonsoft.Json;

  6. using Newtonsoft.Json.Linq;


  7. namespace ConsoleApplication.WebAPI

  8. {

  9.     class Program

  10.     {

  11.         static void Main(string[] args)

  12.         {

  13.             Invoke();

  14.         }


  15.         /// <summary>

  16.         /// Http + Json直接调用

  17.         /// </summary>

  18.         private static void Invoke()

  19.         {

  20.             var result = InvokeHelper.Login();

  21.             var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();

  22.             if (iResult == 1 || iResult == -5)

  23.             {

  24.                 Console.WriteLine("login successed");


  25.                 //result = InvokeHelper.AbstractWebApiBusinessService("Kingdee.BOS.WebAPI.ServiceExtend.ServicesStub.CustomBusinessService.ExecuteService,Kingdee.BOS.WebAPI.ServiceExtend.ServicesStub", null);

  26.                 result = InvokeHelper.AbstractWebApiBusinessService("Kingdee.BOS.WebAPI.ServiceExtend.ServicesStub.CustomBusinessService.ExecuteService", null);


  27.                 {

  28.                     Console.WriteLine(result);

  29.                 }

  30.             }

  31.             else

  32.             {

  33.                 Console.WriteLine("login failed");

  34.             }


  35.             Console.ReadKey();

  36.         }

  37.     }

复制代码


赞 24