分享 C# 操作 MCM 中的 Model 以及 PUSH
发布在APICloud移动应用开发平台2015年4月10日view:2325
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。
    public  class APICloud
{
    public static string AppID = [APPID];
    public static string AppKey = [APPKey];
    public static string UrlBase="https://d.apicloud.com/mcm/api";        public static string PushUrl = "https://p.apicloud.com/api/push/message";

    public static string X_APICloud_AppKey
    {
        get        {

        long amp = (long)(DateTime.Now - new DateTime(1970, 01, 01)).TotalMilliseconds;
        String value = String.Format("{0}UZ{1}UZ{2}", AppID, AppKey, amp);
        byte[] buffer = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(value));
        StringBuilder builder = new StringBuilder();
        foreach (byte num in buffer)
        {
            builder.AppendFormat("{0:x2}", num);
        }
        return builder.ToString() + "." + amp;
    }
    }

    public static class Push {

    /// <summary>
    /// 先手机端推送消息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="content">内容</param>
    /// <param name="type">消息类型,1:消息 2:通知</param>
    /// <param name="groupName">-推送组名.默认:全部组</param>
    /// <param name="platform">0:全部平台,1:ios, 2:android</param>
    /// <param name="timer">定时消息发送时间。定时不为空则为定时消息,毫秒数。可选参数。</param>
    /// <returns></returns>
    public static string push(string title, string content, string type, string groupName = null, string platform = "0", string timer = null)
    {
        string url = PushUrl;
        WebClient webClient = new WebClient();
        webClient.Headers.Add("X-APICloud-AppId", AppID);
        webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
        webClient.Headers.Add("Content-Type", "application/json");
        var PostData = new { title = title, content = content, type = type, platform = platform, groupName = groupName, timer = timer };

        string PostDataStr = System.Web.Helpers.Json.Encode(PostData);
        //string ResponseData = webClient.UploadString(url, "POST", PostDataStr);
        byte[] Data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(PostDataStr);
        byte[] responseData = webClient.UploadData(url, "POST", Data);//得到返回字符流  
        string ResultStr = System.Text.Encoding.GetEncoding("UTF-8").GetString(responseData);//解码  
        return ResultStr;

    }
}

    public static class Model {

        public static string  Get(string ClassName,string ObjectID ,string Encoding) {
            string url = UrlBase + "/" + ClassName+"/"+ObjectID;
            WebClient webClient = new WebClient();
            webClient.Headers.Add("X-APICloud-AppId", AppID);
            webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
            string ResponseData= webClient.DownloadString(url);
            return ResponseData;
        }

        public static string GetAll(string ClassName, string Encoding)
        {
            string url = UrlBase + "/" + ClassName;
            WebClient webClient = new WebClient();
            webClient.Headers.Add("X-APICloud-AppId", AppID);
            webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
            string ResponseData = webClient.DownloadString(url);
            return ResponseData;
        }

        public static string Create(string ClassName, string JsonDataStr, string Encoding)
        {
            string url = UrlBase + "/" + ClassName ;
            byte[] Data = System.Text.Encoding.GetEncoding(Encoding).GetBytes(JsonDataStr);
            WebClient webClient = new WebClient();
            webClient.Headers.Add("X-APICloud-AppId", AppID);
            webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
            webClient.Headers.Add("Content-Type", "application/json");
            byte[] responseData = webClient.UploadData(url, "POST", Data);//得到返回字符流  
            string ResultStr = System.Text.Encoding.GetEncoding(Encoding).GetString(responseData);//解码  
            return ResultStr;
        }

        public static string Edit(string ClassName, string ObjectID, string JsonDataStr, string Encoding)
        {
            string url = UrlBase + "/" + ClassName + "/" + ObjectID;
            byte[] Data = System.Text.Encoding.GetEncoding(Encoding).GetBytes(JsonDataStr);
            WebClient webClient = new WebClient();
            webClient.Headers.Add("X-APICloud-AppId", AppID);
            webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
            webClient.Headers.Add("Content-Type", "application/json");
            byte[] responseData = webClient.UploadData(url, "PUT", Data);//得到返回字符流  
            string ResultStr = System.Text.Encoding.GetEncoding(Encoding).GetString(responseData);//解码  
            return ResultStr;
        }

        public static string Delete(string ClassName, string ObjectID, string Encoding)
        {
            string url = UrlBase + "/" + ClassName + "/" + ObjectID;
            byte[] Data = System.Text.Encoding.GetEncoding(Encoding).GetBytes("");
            WebClient webClient = new WebClient();
            webClient.Headers.Add("X-APICloud-AppId", AppID);
            webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
            byte[] responseData = webClient.UploadData(url, "DELETE", Data);
            string ResultStr = System.Text.Encoding.GetEncoding(Encoding).GetString(responseData);//解码  
            return ResultStr;
        }

    }
}

用户 和 角色 的操作方法 类似

增加 push 接口

    /// <summary>
    /// 手机端推送消息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="content">内容</param>
    /// <param name="type">消息类型,1:消息 2:通知</param>
    /// <param name="groupName">-推送组名.默认:全部组</param>
    /// <param name="platform">0:全部平台,1:ios, 2:android</param>
    /// <param name="timer">定时消息发送时间。定时不为空则为定时消息,毫秒数。可选参数。</param>
    /// <returns></returns>
        public static string push(string title, string content, string type, string groupName = null, string platform = "0", string timer = null)
    {
        string url = PushUrl;
        WebClient webClient = new WebClient();
        webClient.Headers.Add("X-APICloud-AppId", AppID);
        webClient.Headers.Add("X-APICloud-AppKey", X_APICloud_AppKey);
        webClient.Headers.Add("Content-Type", "application/json");
        var PostData = new { title = title, content = content, type = type, platform = platform, groupName = groupName, timer = timer };

        string PostDataStr = System.Web.Helpers.Json.Encode(PostData);
        //string ResponseData = webClient.UploadString(url, "POST", PostDataStr);
        byte[] Data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(PostDataStr);
        byte[] responseData = webClient.UploadData(url, "POST", Data);//得到返回字符流  
        string ResultStr = System.Text.Encoding.GetEncoding("UTF-8").GetString(responseData);//解码  
        return ResultStr;

    }

原帖地址:http://community.apicloud.com/bbs/forum.php?mod=viewthread&tid=504

评论
发表评论
暂无评论
WRITTEN BY
小金宝同學coolboy
我就是太阳无需凭借谁的光!
TA的新浪微博
PUBLISHED IN
APICloud移动应用开发平台
  • 7天开发APP,重新定义移动应用开发。
  • 可以帮助开发者快速实现移动应用的开发、测试、发布、管理和运营的全生命周期管理。
  • 云API:减少70%服务器端编码工作量,同时提供推送、云修复、大数据分析等服务。
  • 端API:“积木拼装”式的开发跨平台APP,为APP开发者提供大量优质的原生模块,涵盖UI、系统、交互、影音、支付等类型的原生模块。
  • 模块store:“模块Store”通过提供“1+1”的服务(一站式+一键集成),让APP开发以及第三方服务SDK的集成变得非常简单,可以像逛超市一样在APICloud“模块Store”挑选自己中意的第三方服务,只需在线一键绑定,就可以实现不同服务的快速集成。
  • 官方网站:www.apicloud.com

友情链接 大搜车前端团队博客
我的收藏