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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = content.gid
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
Info = string.Empty
};
}
else
{
var content = JsonHelper.ToObject<MessageGroup>(result.responseContent);
return new ResultDTO
{
Result = ,
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 = ,
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 = ,
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;
}
}
C#关于HttpClient的应用(二):极光推送IM集成的更多相关文章
- (转载)iOS 极光推送SDK 集成指南
iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...
- ionic中极光推送的集成
1.到极光官网注册账号,新建应用获得appkey. 详见:https://www.jiguang.cn/app/list 2.引入jpush插件 详见:https://github.com/jpush ...
- iOS 极光推送的集成以及一些集成后的狗血
1.首先进入极光文档下载激光推送的SDk---传送门http://docs.jiguang.cn/jpush/client/iOS/ios_sdk/ 将解压后的lib子文件夹(包含JPUSHSer ...
- ios极光推送快速集成教程
内容中包含 base64string 图片造成字符过多,拒绝显示
- 【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)
作者 : octopus_truth 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/45046283 推送技术产生场景 : -- ...
- JPushDemo【极光推送集成,基于v3.1.8版本】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个Demo只是记录极光推送的集成,不能运行. 使用步骤 一.项目组织结构图 注意事项: 1. 导入类文件后需要change包名以 ...
- 极光推送>>java SDK服务端集成后台项目(使用详解)
PS:如果你是第一次用推送,那就直接按照我的步骤来,再去看官方文档,这样,更容易能理解操作流程.还有——-请耐心看 极光文档(java SDK)请参考 [ 极光文档 ] 步骤一: 首先,你必须在 [极 ...
- 李洪强iOS之集成极光推送二iOS 证书 设置指南
李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...
- 极光推送使用实例(二) Android客户端
上一篇简单介绍了极光推送在Java服务端的实现,如果感兴趣的可以看一下极光推送使用实例(一)JAVA服务端.这篇文章介绍下极光推送在Android客户端的实现. JPush Android SDK 是 ...
随机推荐
- 删CentOS / RHEL库和配置文件(Repositories and configuraiton files)
1 删除库简介 随着root权限执行以下的命令: # cd /etc/yum.repos.d/ 列出全部库(repo) #ls CentOS-Base.repo epel.repo mirrors-r ...
- MyEclipse每次修改js有另一种部署
今天在做时间格式显示这块内容的时候.我改动了这个js函数,同一时候凝视的掉了其它的一些替换的方法,可是在浏览器中执行发现效果一点也没有改变.于是debug js,发现浏览器中识别的还是之前未改动的js ...
- android生成apk包出现Unable to add "XXX" Zip add failed问题
最近试图整合umeng至cocos2d-x围棋项目,一切好工作,准备生成apk当出现了大量的数据包 [2014-06-03 20:02:52 - MyApp] Unable to add 'G:\co ...
- MongoDB学习笔记-基础概念
mongodb中基本的概念 文档.集合.数据库 与关系数据库的概念对比更容易理解
- 基于Spring + Spring MVC + Mybatis 高性能web构建
基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...
- .NET Framework 4.0 以上版本 下载地址
https://msdn.microsoft.com/zh-cn/library/5a4x27ek(v=vs.110).aspx
- Cntlm安装和配置体验
对于那些谁使用NTLM验证网络代理环境(即除了需要设置的代理主机和端口还需要提供一个域用户名和密码)供.通过代理上网头疼.这主要是由于非常大的软件不支持NTLM的代理(比方眼下的GIT就不能支持NTL ...
- net搭建热插拔式web框架(沙箱的构建)
net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...
- FTP上传文件时 System.Net.WebException: 基础连接已经关闭: 接收时发生错误。
在c#中使用HttpWebRequest时,频繁请求一个网址时,过段时间就会出现“基础连接已经关闭: 接收时发生意外错误”的错误提示.将webRequest的属性设置成下面的,经测试可以解决.Syst ...
- SDL2来源分析3:渲染(SDL_Renderer)
===================================================== SDL源代码分析系列文章上市: SDL2源码分析1:初始化(SDL_Init()) SDL2 ...