该文档可在文库中在线预览
http://open.kingdee.com/K3cloud/WenKu/DocumentView.aspx?docId=110033
最近因为项目的需要做了一个通过JSON来访问SI的例子和大家一起分享一下;
这里面的关键就是在于调用接口时首先要进行登录,并在登录后持有这次访问的CookieContainer,在下次访问后用同样一个CookieContainer去访问。
分享的例子如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json.Converters;
using test.ServiceReference1;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Web.UI.WebControls;
namespace test
{
///
/// HTTP访问代理
///
///
///
public delegate void HttpClientDelegate(DateTime sender, string content);
///
/// 封装HTTP的访问
///
public class HttpClient
{
///
/// SI方法URL
///
public string Url { get; set; }
///
/// SI内容(方法里面的参数)
///
public string Content { get; set; }
///
/// Cookie,保证登录后,所有访问持有一个Cookie;
///
static CookieContainer _cc = new CookieContainer();
///
/// HTTP访问
///
///
///
///
///
public void SysncRequest(DateTime sendTime, HttpClientDelegate resultCallback, string contentType = "")
{
byte[] postData = UnicodeEncoding.UTF8.GetBytes(Content);
HttpWebRequest httpWebRequest = WebRequest.Create(Url) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentLength = postData.Length;
httpWebRequest.CookieContainer = _cc;
if (contentType != "")
{
httpWebRequest.ContentType = contentType;
}
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();
}
HttpWebResponse httpWebResponse = null;
Stream responseStream = null;
StreamReader streamReader = null;
try
{
httpWebRequest.Timeout = 60 * 60 * 1000;
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
responseStream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(responseStream);
string result = streamReader.ReadToEnd();
resultCallback(sendTime, result);
}
catch (Exception ex)
{
}
finally
{
if (streamReader != null) streamReader.Close();
if (responseStream != null) responseStream.Close();
if (httpWebResponse != null) httpWebResponse.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
Login();
}
///
/// 登陆
///
static void Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = "http://localhost/K3CloudServiceInterface_rhz/json/syncreply/Auth";
httpClient.Content = "{\"provider\":\"credentials\",\"State\":\"\",\"oauth_token\":\"\",\"oauth_verifier\":\"\",\"UserName\":\"Administrator\",\"Password\":\"888888\",\"RememberMe\":true,\"Continue\":\"\",\"nonce\":\"\",\"uri\":\"\",\"response\":\"\",\"qop\":\"\",\"nc\":\"\",\"cnonce\":\"\",\"PasswordIsEncrypted\":false}";
httpClient.SysncRequest(DateTime.Now, CallBack, "application/json; charset=utf-8");
}
///
/// 获取组织
///
static void GetOrgs()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = "http://localhost/K3CloudServiceInterface_rhz/json/syncreply/ORG_Organizations_View";
httpClient.Content = "{\"CreateOrgId\":0,\"Number\":\"100\"}";
httpClient.SysncRequest(DateTime.Now, CallOrgBack, "application/json; charset=utf-8");
}
static void CallBack(DateTime sender, string content)
{
GetOrgs();
}
static void CallOrgBack(DateTime sender, string content)
{
int i = 0;
i++;
}
}
}
推荐阅读