1 Star 0 Fork 0

supermay/CommLib.BdPush

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Baidu_Helper.cs 10.62 KB
一键复制 编辑 原始数据 按行查看 历史
supermay 提交于 2016-08-20 03:52 . no commit message
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace CommLib.BdPush
{
/// <summary>
/// 作者:冼树华
/// QQ:56472465
/// 日期:20150-04-29
/// 功能:百度推送帮助类
/// </summary>
public class Baidu_Helper
{
#region 枚举参数
/// <summary>
/// 设备类型
/// </summary>
public enum Device_Type : uint { Android = 3, IOS = 4 };
/// <summary>
/// 推送类型
/// </summary>
public enum Push_Type : uint { Signle = 1, Multiple = 2, All = 3 };
/// <summary>
/// 消息类型
/// 0:透传消息
/// 1:通知
/// 默认值为0
/// </summary>
public enum Message_Type : uint { Message = 0, Notice = 1 }
/// <summary>
/// 部署状态
/// </summary>
public enum Deploy_Status : uint { Develope = 1, Product = 2 }
//0:tag组播; 1:广播; 2:批量单播; 3:标签组合; 4:精准推送; 5:LBS推送; 6:系统保留; 7:单播;
public enum Query_Type : uint
{
TagPush = 0, Push = 1, BatchPush = 2, TagsPush = 3,
PrecisionPush = 4, LBSPush = 5, SysKeep = 6, SignalPush = 7
}
#endregion
#region http传输方式
public static readonly string HTTP_POST = "post"; //post传输
public static readonly string HTTP_GET = "get"; //get传输
#endregion
#region 相关URL的方法参数
public static readonly string BASE_URL = "http://api.tuisong.baidu.com/rest/3.0/";
public static readonly string PUSH_SIGNLE_DEVICE = "push/single_device"; //推送消息到单台设备
public static readonly string PUSH_ALL = "push/all"; //推送广播消息
public static readonly string PUSH_TAGS = "push/tags"; //推送组播消息
public static readonly string PUSH_BATCH_DEVICE = "push/batch_device"; //推送消息到给定的一组设备(批量单播)
public static readonly string REPORT_QUERY_MSG_STATUS = "report/query_msg_status"; //查询消息的发送状态
public static readonly string REPORT_QUERY_TIMER_RECORDS = "report/query_timer_records"; //查询定时消息的发送记录
public static readonly string REPORT_QUERY_TOPIC_RECORDS = "report/query_topic_records"; //查询指定分类主题的发送记录
public static readonly string APP_QUERY_TAGS = "app/query_tags"; //查询标签组列表
public static readonly string APP_CREATE_TAG = "app/create_tag"; //创建标签组
public static readonly string APP_DEL_TAG = "app/del_tag"; //删除标签组
public static readonly string TAG_ADD_DEVICES = "tag/add_devices"; //添加设备到标签组
public static readonly string TAG_DEL_DEVICES = "tag/del_devices"; //将设备从标签组中移除
public static readonly string TAG_DEVICE_NUM = "tag/device_num"; //查询标签组设备数量
public static readonly string TIMER_QUERY_LIST = "timer/query_list"; //查询定时任务列表
public static readonly string TIMER_CANCEL = "timer/cancel"; //取消定时任务
public static readonly string TOPIC_QUERY_LIST = "topic/query_list"; //查询分类主题列表
public static readonly string REPORT_STATISTIC_MSG = "report/statistic_msg"; //当前应用的消息统计信息
public static readonly string REPORT_STATISTIC_DEVICE = "report/statistic_device"; //当前应用的设备统计信息
public static readonly string REPORT_STATISTIC_TOPIC = "report/statistic_topic"; //查询分类主题统计信息
#endregion
#region 获取签名
private static string CreateSign(string httpMethod, string url, string secretKey, Baidu_Mod mod)
{
string sign = "";
if (mod != null)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
//将键值对按照key的升级排列
var props = mod.GetType().GetProperties().OrderBy(p => p.Name);
string pValue = "";
foreach (var p in props)
{
if (p.GetValue(mod, null) != null)
{
pValue = p.GetValue(mod, null).ToString();
if (pValue != "0")//unint为0是不添加,如广播消息的预定发送时间
dic.Add(p.Name, p.GetValue(mod, null).ToString());
}
}
//生成sign时,不能包含sign标签,所有移除
dic.Remove("sign");
var preData = new StringBuilder();
foreach (var l in dic)
{
preData.Append(l.Key);
preData.Append("=");
preData.Append(l.Value);
}
//按要求拼接字符串,并urlencode编码
var str = HttpUtility.UrlEncode(httpMethod.ToUpper() + url + preData.ToString() + secretKey);
var strSignUpper = new StringBuilder();
int perIndex = 0;
for (int i = 0; i < str.Length; i++)
{
var c = str[i].ToString();
if (str[i] == '%')
{
perIndex = i;
}
if (i - perIndex == 1 || i - perIndex == 2)
{
c = c.ToUpper();
}
strSignUpper.Append(c);
}
// 获取sign的值
sign = Tool.Md5Hash(strSignUpper.ToString()).ToLower();
}
return sign;
}
#endregion
#region 创建参数集合
private static Dictionary<string, string> GetParamerCollection(Baidu_Mod mod, string sign)
{
Dictionary<string, string> paramerCollection = new Dictionary<string, string>();
var props = mod.GetType().GetProperties();
string pValue = "";
foreach (var p in props)
{
if (p.GetValue(mod, null) != null)
{
pValue = p.GetValue(mod, null).ToString();
if (pValue != "0")//unint为0是不添加,如广播消息的预定发送时间
paramerCollection.Add(p.Name, p.GetValue(mod, null).ToString());
}
}
paramerCollection.Add("sign", sign);
return paramerCollection;
}
#endregion
#region 发送消息到百度
/// <summary>
/// 修改SendBaidu适应vs2010
/// </summary>
public static Task<string> SendBaidu(string httpMethod, string url, string secretKey, Baidu_Mod mod)
{
//获取签名
string strSign = CreateSign(httpMethod, url, secretKey, mod);
Task<string> responseContent = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url); //设置WebAPI的地址+类+方法
client.DefaultRequestHeaders.Accept.Clear(); //清空缓存
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")
{
CharSet = "utf-8"
}); //返回值为json和utf-8编码
//POST /rest/3.0/push/single_device HTTP/1.1
//Accept: application/json; charset=utf-8
//User-Agent: BCCS_SDK/3.0 (Darwin; Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64; x86_64) PHP/5.6.3 (Baidu Push Server SDK V3.0.0 and so on..) cli/Unknown ZEND/2.6.0
//Content-Type: application/x-www-form-urlencoded; charset=utf-8
//Host: api.tuisong.baidu.com
//Content-Length: 415
//Expect: 100-continue
var sUserAgent =
@"BCCS_SDK/3.0 (Darwin; Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64; x86_64) PHP/5.6.3 (Baidu Push Server SDK V3.0.0 and so on..) cli/Unknown ZEND/2.6.0";
client.DefaultRequestHeaders.Add("user-agent", sUserAgent);
//响应消息
Task<HttpResponseMessage> response = null;
//判断使用不同的HttpMethod,生成不同的参数,获取相关返回数据
if (HttpMethod.Get.Method.ToLower().Equals(httpMethod.ToLower())) //get方法(暂不提供)
{
}
else if (HttpMethod.Post.Method.ToLower().Equals(httpMethod.ToLower())) //post方法
{
//生成post的参数,并且设置charset为utf-8
Dictionary<string, string> requestDictionary = GetParamerCollection(mod, strSign);
var contents = new FormUrlEncodedContent(requestDictionary);
contents.Headers.ContentType.CharSet = "utf-8";
contents.LoadIntoBufferAsync().Wait();
response = client.PostAsync(url, contents);
}
//返回值
responseContent = (null != response) ? response.Result.Content.ReadAsStringAsync() : null;
} // http完结
return responseContent;
}
#endregion
#region https证书验证
//
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
#endregion
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/supermay/CommLib.BdPush.git
git@gitee.com:supermay/CommLib.BdPush.git
supermay
CommLib.BdPush
CommLib.BdPush
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385