本文介绍了如何在不引用“Kingdee.BOS.WebApi.Client.dll”组件的情况下,通过K/3 Cloud Web API进行开发,并提供了示例代码。文章旨在提供一个通用的Web API接口调用方法,以解决该组件仅支持.NET Framework 4.0版本及非.NET语言开发引用的问题。示例包括服务端和客户端的自定义代码,展示了如何通过HTTP请求进行服务调用,并提供了登录、保存和删除等操作的封装实现。服务端代码使用.NET C#编写,客户端代码同样以.NET C#为例,但强调了任何支持HTTP协议的语言均可实现类似封装。
先上张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# 服务端个性需求自定义代码:
/*
* 服务需要引用组件如下
* 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;
}
}
}
复制代码
如下是客户端代码:是一个轻量级的工程,建立.Net C# 控制台程序
http请求封装:
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
{
/// <summary>
/// Seivice URL
/// </summary>
public string Url { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// Cookie,保证登录后,所有访问持有一个Cookie;
/// </summary>
static CookieContainer Cookie = new CookieContainer();
/// <summary>
/// HTTP访问
/// </summary>
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;
}
}
}
复制代码
操作辅助类封装:
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 业务站点地址
/// <summary>
/// 登陆
/// </summary>
public static string Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");
List<object> Parameters = new List<object>();
Parameters.Add("558cbb01bfc79b");//帐套Id
Parameters.Add("Administrator");//用户名
Parameters.Add("888888");//密码
Parameters.Add(2052);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}
/// <summary>
/// 保存
/// </summary>
/// <param name="formId"></param>
/// <param name="content"></param>
/// <returns></returns>
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<object> Parameters = new List<object>();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="formId"></param>
/// <param name="content"></param>
/// <returns></returns>
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<object> Parameters = new List<object>();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}
/// <summary>
/// 审核
/// </summary>
/// <param name="formId"></param>
/// <param name="content"></param>
/// <returns></returns>
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<object> Parameters = new List<object>();
//业务对象Id
Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}
/// <summary>
/// 自定义
/// </summary>
/// <param name="key">自定义方法标识</param>
/// <param name="args">参数</param>
/// <returns></returns>
public static string AbstractWebApiBusinessService(string key, List<object> args)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat(CloudUrl, key, ".common.kdsvc");
httpClient.Content = JsonConvert.SerializeObject(args);
return httpClient.SysncRequest();
}
}
}
复制代码
调用代码:
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();
}
/// <summary>
/// Http + Json直接调用
/// </summary>
private static void Invoke()
{
var result = InvokeHelper.Login();
var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
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();
}
}
复制代码
推荐阅读