using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string baseUrl = "http://192.168.1.1/k3cloud";//服务器地址
HttpClientEx httpClient = new HttpClientEx();
httpClient.Url = string.Format("{0}/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc", baseUrl);
List<object> Parameters = new List<object>();
Parameters.Add("11111111");//帐套Id
Parameters.Add("demo");//用户名
Parameters.Add("123456");//密码
Parameters.Add(2052);//多语言:中文
httpClient.Content = JsonConvert.SerializeObject(Parameters);
var iResult = JObject.Parse(httpClient.AsyncRequest())["LoginResultType"].Value<int>();
if (iResult == 1)
{
while (true)
{
Console.WriteLine("请输入任意字符回车继续,输入【exit】退出!");
string key = Console.ReadLine();
if (key == "exit") break;
httpClient.Url = string.Format("{0}/Kingdee.K3.SCM.WebApi.ServicesStub.InventoryQueryService.GetInventoryData.common.kdsvc", baseUrl);
Parameters = new List<object>();
InventoryParamModel model = new InventoryParamModel();
model.fstocknumbers = "CK001,CK002,CK003";
model.isshowauxprop = true;
model.isshowstockloc = true;
model.pageindex = 1;
model.pagerows = 2;
Parameters.Add(model);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
string response = httpClient.AsyncRequest();
Console.WriteLine(string.Format("结果={0}", response));
}
}
}
}
class InventoryParamModel
{
/// <summary>
/// 库存组织编码,多个使用英文逗号【,】分隔
/// </summary>
public string fstockorgnumbers { get; set; }
/// <summary>
/// 物料编码,多个使用英文逗号【,】分隔
/// </summary>
public string fmaterialnumbers { get; set; }
/// <summary>
/// 仓库编码,多个使用英文逗号【,】分隔
/// </summary>
public string fstocknumbers { get; set; }
/// <summary>
/// 批号编码,多个使用英文逗号【,】分隔
/// </summary>
public string flotnumbers { get; set; }
/// <summary>
/// 是否查询仓位
/// </summary>
public bool isshowstockloc { get; set; }
/// <summary>
/// 是否查询辅助属性
/// </summary>
public bool isshowauxprop { get; set; }
/// <summary>
/// 当前页码
/// </summary>
public int pageindex { get; set; }
/// <summary>
/// 每页显示行数
/// </summary>
public int pagerows { get; set; }
}
class HttpClientEx
{
/// <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 AsyncRequest()
{
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:"))
{
return responseText.TrimStart("response_error:".ToCharArray());
}
return responseText;
}
}
}
标签:set,报表,金蝶,get,K3,Add,using,public,string
From: https://www.cnblogs.com/ives/p/17965603