• C#关于HttpClient的应用(二):极光推送IM集成


        public class JPushClient:BaseHttpClient
        {
            private String appKey;
            private String masterSecret;
    
            public JPushClient()
            {
                this.appKey = BaseHelper.GetValue("PushMessageAppKey");
                this.masterSecret = BaseHelper.GetValue("PushMessageMasterSecret");
            }
    
            public JPushClient(String appKey, String masterSecret)
            {
                this.appKey = appKey;
                this.masterSecret = masterSecret;
            }
    
            /// <summary>
            /// HTTP 验证
            /// </summary>
            /// <returns></returns>
            public override Dictionary<string,string> Authorization()
            {
                var auth = BaseHelper.GetBase64Encode(this.appKey + ":" + this.masterSecret);
                return new Dictionary<string, string> { { "Authorization","Basic "+ auth } };
            }
        }

    Server层:

        public class JMessageServer
        {
            #region 请求地址
    
            private const String HOST_PATH = "https://api.im.jpush.cn";
            private const String USERS_PATH = HOST_PATH + "/v1/users/";
            private const String GROUP_PATH = HOST_PATH + "/v1/groups/";
            private const String Message_PATH = HOST_PATH + "/v1/messages";
            private const String ADMIN_PATH = HOST_PATH + "/v1/admins/";
    
            #endregion
    
            /// <summary>
            /// 添加多个用户
            /// </summary>
            /// <param name="users"></param>
            /// <returns></returns>
            public bool AddUsers(List<JUserDTO> users)
            {
                String url = USERS_PATH;
                JPushClient client=new JPushClient();
                //ResponseWrapper result = sendPost(url, Authorization(), JsonHelper.ToJson(users));
    
                if (result.responseCode == HttpStatusCode.Created)
                {
                    return true;
                }
                else
                {
                    //移除已经注册过的用户,重新添加
                    var content = JsonHelper.ToObject<List<MessageUser>>(result.responseContent);
                    bool flag = true;
                    foreach (var item in content)
                    {
                        if (item.error.code == (int)EnumMessageError.用户已存在)
                        {
                            var user = users.First(p => p.username == item.username);
                            users.Remove(user);
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        AddUsers(users);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
    
            /// <summary>
            /// 添加单个用户
            /// </summary>
            /// <param name="users"></param>
            /// <returns></returns>
            public ResultDTO AddUser(JUserDTO users)
            {
                String url = HOST_NAME_SSL;
                url += USERS_PATH;
    
                ResponseWrapper result = sendPost(url, Authorization(), "[" + JsonHelper.ToJson(users) + "]");
    
                if (result.responseCode == HttpStatusCode.Created)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 添加管理员(注:管理员不能和用户重复注册)
            /// </summary>
            /// <param name="users"></param>
            /// <returns></returns>
            public ResultDTO AddAdmin(JUserDTO users)
            {
                String url = HOST_NAME_SSL;
                url += ADMIN_PATH;
    
                ResponseWrapper result = sendPost(url, Authorization(), JsonHelper.ToJson(users));
    
                if (result.responseCode == HttpStatusCode.Created)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 获取管理员列表
            /// </summary>
            /// <param name="start"></param>
            /// <param name="count"></param>
            /// <returns></returns>
            public string GetAdminsList(int start, int count)
            {
                String url = HOST_NAME_SSL;
                url += ADMIN_PATH + string.Format("?start={0}&count={1}", start, count);
    
                ResponseWrapper result = sendGet(url, Authorization(), null);
                return result.responseContent;
            }
    
            //修改用户昵称
            public ResultDTO UpdateUserInfo(string userId, string nickName)
            {
                String url = HOST_NAME_SSL;
                url += USERS_PATH + userId;
                var param = "{"nickname":"" + nickName + ""}";
                ResponseWrapper result = sendPut(url, Authorization(), param);
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 获取用户信息
            /// </summary>
            /// <param name="userId"></param>
            /// <returns></returns>
            public string GetUserInfo(string userId)
            {
                String url = HOST_NAME_SSL;
                url += USERS_PATH + userId;
    
                ResponseWrapper result = sendGet(url, Authorization(), null);
                return result.responseContent;
            }
    
            /// <summary>
            /// 修改登录密码
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="password"></param>
            /// <returns></returns>
            public ResultDTO ModifyLoginPassword(string userId, string password)
            {
                String url = HOST_NAME_SSL;
                url += USERS_PATH + userId + "/password";
                var param = "{"new_password":"" + password + ""}";
                ResponseWrapper result = sendPut(url, Authorization(), param);
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 创建群组
            /// </summary>
            /// <param name="group"></param>
            /// <returns></returns>
            public ResultDTO AddGroup(JGroupDTO group)
            {
                String url = HOST_NAME_SSL;
                url += GROUP_PATH;
    
                ResponseWrapper result = sendPost(url, Authorization(), JsonHelper.ToJson(group));
                if (result.responseCode == HttpStatusCode.Created)
                {
                    var content = JsonHelper.ToObject<JGroupDTO>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = content.gid
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 删除群组
            /// </summary>
            /// <param name="gid">群组ID</param>
            public ResultDTO DeleteGroup(string gid)
            {
                String url = HOST_NAME_SSL;
                url += GROUP_PATH;
    
                ResponseWrapper result = sendDelete(url + gid, Authorization(), "");
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 退出群组
            /// </summary>
            /// <param name="gid">群组ID</param>
            /// <param name="userId">用户ID</param>
            /// <returns></returns>
            public ResultDTO DeleteGroupUser(string gid, string userId)
            {
                String url = HOST_NAME_SSL;
                url += GROUP_PATH + gid + "/members";
                JGroupUserDTO dto = new JGroupUserDTO();
                dto.remove = new[] { userId };
                var param = JsonHelper.ToJson(dto);
                ResponseWrapper result = sendPost(url, Authorization(), param);
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 添加群组成员
            /// </summary>
            /// <param name="gid">群组ID</param>
            /// <param name="userId">用户ID</param>
            /// <returns></returns>
            public ResultDTO AddGroupUser(string gid, string userId)
            {
                String url = HOST_NAME_SSL;
                url += GROUP_PATH + gid + "/members";
                JGroupUserDTO dto = new JGroupUserDTO();
                dto.add = new[] { userId };
                var param = JsonHelper.ToJson(dto);
                ResponseWrapper result = sendPost(url, Authorization(), param);
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
    
            /// <summary>
            /// 更新群组成员
            /// </summary>
            /// <param name="gid">群组ID</param>
            /// <param name="addUsers">新增的成员IDs</param>
            /// <param name="removeUsers">移除的群组IDs</param>
            /// <returns></returns>
            public ResultDTO UpdateGroupUsers(string gid, string[] addUsers, string[] removeUsers)
            {
                String url = HOST_NAME_SSL;
                url += GROUP_PATH + gid + "/members";
                JGroupUserDTO dto = new JGroupUserDTO { remove = removeUsers, add = addUsers };
                var param = JsonHelper.ToJson(dto);
                ResponseWrapper result = sendPost(url, Authorization(), param);
                if (result.responseCode == HttpStatusCode.NoContent)
                {
                    return new ResultDTO
                    {
                        Result = 1,
                        Info = string.Empty
                    };
                }
                else
                {
                    var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
                    return new ResultDTO
                    {
                        Result = 0,
                        Info = BaseHelper.GetEnumKey<EnumMessageError>(content.error.code)
                    };
                }
            }
    
            /// <summary>
            /// 发送个人信息
            /// </summary>
            /// <param name="fromUserId"></param>
            /// <param name="toUserId"></param>
            /// <param name="content"></param>
            /// <returns></returns>
            public string SendSingleMessage(string fromUserId, string toUserId, MsgContent content)
            {
                String url = HOST_NAME_SSL;
                url += Message_PATH;
                var message = new JMessageDTO
                {
                    version = 1,
                    target_type = JTargetType.single.ToString(),
                    from_type = JFromType.admin.ToString(),
                    msg_type = JMsgType.text.ToString(),
                    from_id = fromUserId,
                    target_id = toUserId,
                    msg_body = content
                };
                var param = JsonHelper.ToJson(message);
                ResponseWrapper result = sendPost(url, Authorization(), param);
                return result.responseContent;
    
            }
    
            /// <summary>
            /// 发送群组信息
            /// </summary>
            /// <param name="fromUserId"></param>
            /// <param name="gid"></param>
            /// <param name="content"></param>
            /// <returns></returns>
            public string SendGroupMessage(string fromUserId, string gid, MsgContent content)
            {
                String url = HOST_NAME_SSL;
                url += Message_PATH;
                var message = new JMessageDTO
                {
                    version = 1,
                    target_type = JTargetType.group.ToString(),
                    from_type = JFromType.admin.ToString(),
                    msg_type = JMsgType.text.ToString(),
                    from_id = fromUserId,
                    target_id = gid,
                    msg_body = content
                };
                var param = JsonHelper.ToJson(message);
                ResponseWrapper result = sendPost(url, Authorization(), param);
                return result.responseContent;
            }
        }
  • 相关阅读:
    郑码
    AutoCAD 安装
    China Mobile 移动
    CCB 建行
    Word基础
    Java 继承
    Java 封装(内部类)
    Java 类与对象
    Java 方法
    Java 数组
  • 原文地址:https://www.cnblogs.com/xuhang/p/5204974.html
Copyright © 2020-2023  润新知