当Cloud服务站点发布成功之后,一般输入地址"http://localhost/K3CloudServiceInterface/metadata",会出现类似下图,可以看到提供调用的接口有Json方式、soap1.1及soap1.2方式。
本次分享主要为了说明通过Json方式保存数据,构造数据时需要注意的地方 主要2点:
1、NeedUpDateFields 为需要保存的字段列表 Json数组方式,必须构造。
2、当NeedUpDateFields包含需要保存的单据体字段,则需添加单据体标识 结构为 [业务对象Id]__[单据体标识],单据体字段。
如下示例代码,是通过Json方式保存【销售订单】,把其中的【客户编号】【销售员编号】【物料编号】替换正确即可保存。
来一张具有Json缩进格式的数据效果图。
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication.Json
{
class Program
{
static void Main(string[] args)
{
if (Login())
{
//业务对象Id 销售订单 SAL_SaleOrder
//Json字串
string sFormId = "SAL_SaleOrder";
string sJson = "{\"Creator\":\"String\",\"NeedUpDateFields\":[\"FBillTypeID\",\"FDate\",\"FBusinessType\",\"FSaleOrgId\",\"FCustId\",\"FSettleCurrId\",\"FSalerId\",\"SAL_SaleOrder__FSaleOrderEntry\",\"FMaterialId\",\"FSettleOrgIds\",\"FUnitID\",\"FQty\"],\"Model\":{\"FID\":0,\"FBillTypeID\":{\"FNumber\":\"XSDD01_SYS\"},\"FBusinessType\":\"NORMAL\",\"FSaleOrgId\":{\"FNUMBER\":\"100\"},\"FCustId\":{\"FNUMBER\":\"客户编号\"},\"FSettleCurrId\":{\"FNUMBER\":\"PRE001\"},\"FSalerId\":{\"FNUMBER\":\"销售员编号\"},\"SAL_SaleOrder__FSaleOrderEntry\":{\"FMaterialId\":{\"FNUMBER\":\"物料编号\"},\"FSettleOrgIds\":{\"FNUMBER\":\"100\"},\"FUnitID\":{\"FNumber\":\"Pcs\"},\"FQty\":10}}}";
Save(sFormId, sJson);
}
}
///
/// 登陆
///
static bool Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = "http://localhost/K3CloudServiceInterface/json/syncreply/Auth";
httpClient.Content = "{\"provider\":\"credentials\",\"UserName\":\"Administrator\",\"Password\":\"888888\",\"PasswordIsEncrypted\":false,\"RememberMe\":false}";
var result = httpClient.SysncRequest();
var bResult = JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].Value
return bResult;
}
///
/// 保存
///
static void Save(string formId, string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://localhost/K3CloudServiceInterface/json/syncreply/", formId, "_Save");
httpClient.Content = content;
var result = httpClient.SysncRequest();
}
}
}
封装Http请求代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication.Json
{
///
/// HTTP访问
///
public class HttpClient
{
///
/// URL
///
public string Url { get; set; }
///
/// Content
///
public string Content { get; set; }
///
/// Cookie
///
static CookieContainer Cookie = new CookieContainer();
///
/// HTTP访问
///
public string SysncRequest()
{
HttpWebRequest httpWebRequest = WebRequest.Create(Url) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.CookieContainer = Cookie;
httpWebRequest.ContentType = "Json";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
byte[] postData = UnicodeEncoding.UTF8.GetBytes(Content);
requestStream.Write(postData, 0, postData.Length);
requestStream.Flush();
}
using (var repStream = httpWebRequest.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(repStream))
{
string responseText = reader.ReadToEnd();
JObject jo = JsonConvert.DeserializeObject(responseText) as JObject;
return jo.ToString();
}
}
}
}
}
k3CloudJson.png(31.25KB)
k3CloudJsonModel.png(13.26KB)
推荐阅读