批量查询用户表数据
最主要的是构造CreateQuery的Model模板,设置需要返回的字段数据。
先上Cloud中 用户表,按字段【用户名】批量查找查询结果,查询结果最终也是返回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())
{
string sJsonCreate = "{\"Model\":{\"FormId\":\"SEC_User\",\"HeadEntity\":{\"EntityKey\":\"SEC_User\",\"Fields\":[{\"FieldKey\":\"FName\"}]}}}";
var QueryId = CreateQuery(sJsonCreate);
string sJsonExecute = "{\"QueryId\":\"" + QueryId + "\",\"FilterString\":\"\",\"Recorders\":{\"From\":0,\"To\":1000},\"OrderString\":\"\"}";
ExecuteQuery(sJsonExecute);
string sJsonDispose = "{\"QueryId\":\"" + QueryId + "\"}";
DisposeQuery(sJsonDispose);
}
}
///
/// 登陆
///
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 string CreateQuery(string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://localhost/K3CloudServiceInterface/json/syncreply/CreateQuery");
httpClient.Content = content;
var result = httpClient.SysncRequest();
var bResult = JObject.Parse(result)["Result"]["QueryId"].Value
return bResult;
}
static void ExecuteQuery(string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://localhost/K3CloudServiceInterface/json/syncreply/ExecuteQuery");
httpClient.Content = content;
var result = httpClient.SysncRequest();
}
static void DisposeQuery(string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://localhost/K3CloudServiceInterface/json/syncreply/DisposeQuery");
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();
}
}
}
}
}
k3CloudJsonBatch.png(5.29KB)
推荐阅读