K/3 Cloud Web API集成开发客户端不引用组件示例【分享】
金蝶云社区-云社区用户4u731234
云社区用户4u731234
1人赞赏了该文章 3.4万次浏览 未经作者许可,禁止转载编辑于2020年02月13日 08:41:06
summary-icon摘要由AI智能服务提供

本文介绍了如何不引用"Kingdee.BOS.WebApi.Client.dll"组件进行K/3 Cloud Web API开发,提供了通用的接口调用示例代码。由于原组件仅支持Framework4.0,且仅限于.Net C#开发,本文提供的方法支持任何支持HTTP协议的语言。示例中包含了服务端和客户端代码,其中服务端代码展示了如何扩展K/3 Cloud组件,客户端代码则演示了如何封装HTTP请求进行接口调用。文章还展示了如何封装操作辅助类以方便调用不同的API服务,并给出了一个具体的调用示例来展示如何使用这些代码。

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

本帖子达到目的:一箭三雕
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# 服务端个性需求自定义代码:
[code]/*
* 服务需要引用组件如下
* Kingdee.BOS.dll、Kingdee.BOS.ServiceFacade.KDServiceFx.dll、Kingdee.BOS.WebApi.ServicesStub.dll
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.WebApi.ServicesStub;
using Kingdee.BOS.JSON;

namespace Kingdee.BOS.WebAPI.ServiceExtend.ServicesStub
{
public class CustomBusinessService : AbstractWebApiBusinessService
{
public CustomBusinessService(KDServiceContext context)
: base(context)
{ }

public JSONArray ExecuteService(string parameter)
{
JSONArray jsonArray = new JSONArray();
jsonArray.Add("Hello World");
return jsonArray;
}
}
}[/code]

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

http请求封装:
[code]using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ConsoleApplication.WebAPI
{
class HttpClient
{
///


/// Seivice URL
///

public string Url { get; set; }
///
/// 内容
///

public string Content { get; set; }
///
/// Cookie,保证登录后,所有访问持有一个Cookie;
///

static CookieContainer Cookie = new CookieContainer();

///


/// HTTP访问
///

public string SysncRequest()
{
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.CookieContainer = Cookie;
httpRequest.Timeout = 1000 * 60 * 10;//10min

using (Stream reqStream = httpRequest.GetRequestStream())
{
JObject jObj = new JObject();
jObj.Add("format", 1);
jObj.Add("useragent", "ApiClient");
jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());
jObj.Add("parameters", Content);
jObj.Add("timestamp", DateTime.Now);
jObj.Add("v", "1.0");
string sContent = jObj.ToString();
var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Flush();
}
using (var repStream = httpRequest.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(repStream))
{
return ValidateResult(reader.ReadToEnd());
}
}
}

private static string ValidateResult(string responseText)
{
if (responseText.StartsWith("response_error:"))
{
var failText = responseText.TrimStart("response_error:".ToCharArray());
}
return responseText;
}
}
}
[/code]
操作辅助类封装:
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace ConsoleApplication.WebAPI
{
public static class InvokeHelper
{
private static string CloudUrl = "http://192.168.66.60/k3cloud/";//K/3 Cloud 业务站点地址

///


/// 登陆
///

public static string Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");

List Parameters = new List();
Parameters.Add("558cbb01bfc79b");//帐套Id
Parameters.Add("Administrator");//用户名
Parameters.Add("888888");//密码

Parameters.Add(2052);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}

///


/// 保存
///

///
///
///
public static string Save(string formId, string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");

List Parameters = new List();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}

///


/// 删除
///

///
///
///
public static string Delete(string formId, string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc");

List Parameters = new List();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}

///


/// 审核
///

///
///
///
public static string Audit(string formId, string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");

List Parameters = new List();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}

///


/// 自定义
///

/// 自定义方法标识
/// 参数
///
public static string AbstractWebApiBusinessService(string key, List args)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, key, ".common.kdsvc");

httpClient.Content = JsonConvert.SerializeObject(args);
return httpClient.SysncRequest();
}
}
}
[/code]
调用代码:
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication.WebAPI
{
class Program
{
static void Main(string[] args)
{
Invoke();
}

///


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

private static void Invoke()
{
var result = InvokeHelper.Login();
var iResult = JObject.Parse(result)["LoginResultType"].Value();
if (iResult == 1 || iResult == -5)
{
Console.WriteLine("login successed");

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

{
Console.WriteLine(result);
}
}
else
{
Console.WriteLine("login failed");
}

Console.ReadKey();
}
}[/code]

WebAPIWN.png(10.17KB)