Ejemplo n.º 1
0
        public string PushMessage(PushOptions opts)
        {
            this.opts = opts;
            Dictionary <string, string> dic = new Dictionary <string, string>();
            //将键值对按照key的升级排列
            var props = typeof(PushOptions).GetProperties().OrderBy(p => p.Name);

            foreach (var p in props)
            {
                if (p.GetValue(this.opts, null) != null)
                {
                    dic.Add(p.Name, p.GetValue(this.opts, 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(this.httpMehtod.ToUpper() + this.url + preData.ToString() + this.secret_key, System.Text.Encoding.UTF8);

            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);
            }

            strSignUpper = strSignUpper.Replace("(", "%28").Replace(")", "%29");


            var sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSignUpper.ToString(), "MD5").ToLower();

            //加入生成好的sign键值对
            dic.Add("sign", sign);
            var strb = new StringBuilder();

            //int tagIndex = 0;
            foreach (var l in dic)
            {
                strb.Append(l.Key);
                strb.Append("=");
                strb.Append(l.Value);
                strb.Append("&");
            }

            var postStr = strb.ToString().EndsWith("&") ? strb.ToString().Remove(strb.ToString().LastIndexOf('&')) : strb.ToString();


            byte[]    data      = Encoding.UTF8.GetBytes(postStr);//编码,尤其是汉字,事先要看下抓取网页的编码方式
            WebClient webClient = new WebClient();

            try
            {
                if (this.httpMehtod.Equals("POST"))
                {
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); //采取POST方式必须加的header
                }
                byte[] responseData = webClient.UploadData(this.url, "POST", data);             //得到返回字符流
                string srcString    = Encoding.UTF8.GetString(responseData);                    //解码
                return(srcString);
            }
            catch (WebException ex)
            {
                Stream stream = ex.Response.GetResponseStream();
                string m      = ex.Response.Headers.ToString();
                byte[] buf    = new byte[256];
                stream.Read(buf, 0, 256);
                stream.Close();
                int count = 0;
                foreach (var b in buf)
                {
                    if (b > 0)
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
                return("Post:" + postStr + "\r\n\r\n" + "Response:" + Encoding.UTF8.GetString(buf, 0, count));
                //return  "Post:" + postStr + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ApiKey"></param>
        /// <param name="SecretKey"></param>
        /// <param name="sandbox"></param>
        /// <param name="platform">1:苹果 2:安卓</param>
        /// <param name="BDUserID"></param>
        /// <param name="BDChannelID"></param>
        /// <returns></returns>
        public static string SendPush(string ApiKey, string SecretKey, bool sandbox, int platform, string BDUserID, string BDChannelID, string NoticeTitle, string NoticeContent, string custom_content)
        {
            //当前APIKEY
            string sk = SecretKey;
            //当前SecreKey
            string ak = ApiKey;

            //百度推送
            BaiduPush Bpush    = new BaiduPush("POST", sk);
            String    apiKey   = ak;
            String    messages = "";
            String    method   = "push_msg";//默认消息推送

            //当前时间戳为 PushOptions服务
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
            //默认设备为安卓

            /*
             * 云推送支持多种设备,各种设备的类型编号如下:
             *  3:Andriod设备;
             *  4:iOS设备;
             *  如果存在此字段,则向指定的设备类型推送消息。 默认为android设备类型。
             */
            uint device_type = platform == 1 ? (uint)4 : (uint)3;
            //获取总秒数
            uint unixTime = (uint)ts.TotalSeconds;
            //当前消息类型
            uint message_type;
            //消息签名一一对应
            string messageksy = "xxx000";

            //通知
            message_type = 1;//0:消息;1:通知。默认为0

            //如果选择的是通知并且是苹果,那么设备类型改变为4
            if (platform == 1) //1:苹果 2:安卓
            {
                IOSNotification notification = new IOSNotification();
                notification.title       = NoticeContent;
                notification.description = NoticeTitle;
                messages = notification.getJsonString();
            }
            else
            {
                BaiduPushNotification notification = new BaiduPushNotification();
                notification.title          = NoticeTitle;
                notification.description    = NoticeContent;
                notification.custom_content = custom_content;
                messages = notification.getJsonString();
            }
            PushOptions pOpts = new PushOptions(method, apiKey, BDUserID, BDChannelID, device_type, messages, messageksy, unixTime);

            //消息或则通知
            pOpts.message_type = message_type;

            //如果是苹果发布版本
            if (platform == 1 && sandbox)
            {
                //则当前是开发状态
                pOpts.deploy_status = 1;
            }
            else if (platform == 1 && !sandbox)//如果是苹果开发版本
            {
                //则当前是生产状态
                pOpts.deploy_status = 2;
            }
            string response = Bpush.PushMessage(pOpts);

            return(response);
        }