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集成的更多相关文章

  1. (转载)iOS 极光推送SDK 集成指南

    iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...

  2. ionic中极光推送的集成

    1.到极光官网注册账号,新建应用获得appkey. 详见:https://www.jiguang.cn/app/list 2.引入jpush插件 详见:https://github.com/jpush ...

  3. iOS 极光推送的集成以及一些集成后的狗血

    1.首先进入极光文档下载激光推送的SDk---传送门http://docs.jiguang.cn/jpush/client/iOS/ios_sdk/   将解压后的lib子文件夹(包含JPUSHSer ...

  4. ios极光推送快速集成教程

    内容中包含 base64string 图片造成字符过多,拒绝显示

  5. 【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)

    作者 : octopus_truth 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/45046283 推送技术产生场景 : -- ...

  6. JPushDemo【极光推送集成,基于v3.1.8版本】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个Demo只是记录极光推送的集成,不能运行. 使用步骤 一.项目组织结构图 注意事项: 1.  导入类文件后需要change包名以 ...

  7. 极光推送>>java SDK服务端集成后台项目(使用详解)

    PS:如果你是第一次用推送,那就直接按照我的步骤来,再去看官方文档,这样,更容易能理解操作流程.还有——-请耐心看 极光文档(java SDK)请参考 [ 极光文档 ] 步骤一: 首先,你必须在 [极 ...

  8. 李洪强iOS之集成极光推送二iOS 证书 设置指南

    李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...

  9. 极光推送使用实例(二) Android客户端

    上一篇简单介绍了极光推送在Java服务端的实现,如果感兴趣的可以看一下极光推送使用实例(一)JAVA服务端.这篇文章介绍下极光推送在Android客户端的实现. JPush Android SDK 是 ...

随机推荐

  1. 第九讲:HTML5该canvas推箱子原型实现

    <html> <head> <title>动</title> <script src="../js/jscex.jscexRequire ...

  2. 【Web探索之旅】第二部分第三课:框架和内容管理系统

    内容简介 1.第二部分第三课:框架和内容管理系统 2.第二部分第四课预告:数据库   第二部分第三课:框架和内容管理系统 上一课我们介绍了服务器端的编程语言,有PHP,Java,Python,Ruby ...

  3. [LeetCode235]Lowest Common Ancestor of a Binary Search Tree

    题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...

  4. Metatable和Metamethod(转)

    Metatable和Metamethod是用来干啥的?它们可以使得表a和b的表达式“a + b”变得有意义,其中metatable使两个不相关的表a和b之间可以进行操作,而操作的具体行为比如说&quo ...

  5. 获取activity的根视图

    Activity的根视图是什么? Activity所谓的根视图,就是Activity的最底层的View,也就是在Acitivty创建的时候setContentView的时候传入的View. 如何获取到 ...

  6. mac eclipse svn subeclipse: Failed to load JavaHL Library.

    Failed to load JavaHL Library. These are the errors that were encountered: no libsvnjavahl-1 in java ...

  7. 翻译器DIY————次序

    突然有一种冲动,想要写一个编译器. 因此,检查在网上搜索相关信息,思想direct3D 有本书叫龙,也有个龙书 Compilers Principles,Techniques, & Tool ...

  8. 2C 产品的本质是人性,2B 产品的背后是业务(转)

    本文作者李源是 BLUES 原来做 YY 语音客户端产品时候的同事,原来针对 YY 语音的游戏用户做 2C 的 PC 客户端产品和 APP,后来到某品牌手机做 2B 的后台系统.以下文章,是作者经历了 ...

  9. Linux之父:除了写内核代码 别的真不会(转)

    Linus Torvalds 是 Linux 之父,被誉为活着的传奇. 其实很多人不了解他也并不奇怪,因为人是在是太低调了.现年 46 岁的他每天的工作仍然是编程,领导并推动着 Linux 的发展. ...

  10. C#软件开发实例.个人定制自己的屏幕抓图工具(八)加入了截图功能键盘

    章文件夹 (一)功能概览 (二)创建项目.注冊热键.显示截图主窗体 (三)托盘图标及菜单的实现 (四)基本截图功能实现 (五)针对拖拽时闪烁卡顿现象的优化 (六)加入配置管理功能 (七)加入放大镜的功 ...