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. 多功能截图工具(WinSnap)4.5.6 绿色汉化版(附注册码)

    http://www.uzzf.com/Soft/9840.html 注册名:www.uzzf.com 注册码:FGE5ML-XD2C0G33-GCMDLRB5

  2. freemarker错误九

    1.错误叙述性说明 五月 30, 2014 11:52:04 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...

  3. Win10打不开chm文件的解决办法

    在Win10系统中打开chm文件时,提示安全警告,如下图: 点击打开后,可以看到chm文件的目录大纲,但是点击任意目录其内容都是空白,如下图: 经过网络一通搜索,发现解决办法很简单,就是在上面的安装警 ...

  4. [webapi] 如何在查看api时 显示api的说明

    首先在Controller的方法中 写上相关注释,如下图 然后 右击webapi项目点属性.按照下图选择 红色框中内容要保持一致 然后保存. 在项目中找到到这个文件Areas/HelpPage/App ...

  5. [原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇)

    原文:[原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) .NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) 前言:接着上篇来. 系列文章链接: [ ...

  6. unix &amp; linux oralce用户 内存使用情况分析

    Linux*********************************************************************************************** ...

  7. SQL Server 2008 (R2) 单机版安装的先决条件

    原文:SQL Server 2008 (R2) 单机版安装的先决条件 出自:http://blogs.msdn.com/b/apgcdsd/archive/2012/03/07/sql-server- ...

  8. java假设去请求一个网页的数据

    我们能够通过在java程序中模拟浏览器一样,把数据抓下来,详细方法是在java程序中set header和cookie,以下是一个样例: public class NetConnection { pu ...

  9. hdu4570Multi-bit Trie (间隙DP)

    Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...

  10. 分析Cocos2d-x横版ACT手游源 1、登录

    我自己的游戏代码 因为 游戏源 盯着外面的 我们能够能够理解 /******************************************************************** ...