Exemple #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
            {
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); //采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
                byte[] responseData = webClient.UploadData(this.url, "POST", data);         //得到返回字符流
                string srcString    = Encoding.UTF8.GetString(responseData);                //解码
                return("Post:" + postStr + "\r\n\r\n" + "Response:" + 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 + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count));
            }
        }
Exemple #2
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
            {
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
                byte[] responseData = webClient.UploadData(this.url, "POST", data);//得到返回字符流
                string srcString = Encoding.UTF8.GetString(responseData);//解码
                return "Post:" + postStr + "\r\n\r\n" + "Response:" + 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 + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count);
            }
        }
Exemple #3
0
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string secretKey = PubSql.SecretKey;
                string apiKey = PubSql.ApiKey;

                CodeBehind.BaiduPush Bpush = new CodeBehind.BaiduPush("POST", secretKey);
                String messages = "";
                String method = "push_msg";
                TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                uint device_type = 3;
                uint unixTime = (uint) ts.TotalSeconds;

                uint message_type;
                string messageksy = "xxxxxx";
                if (RbMessage.Checked)
                {
                    message_type = 0;
                    messages = TBMessage.Text;
                }
                else
                {
                    message_type = 1;

                    if (RBIOSPRO.Checked == true || RBIOSDEV.Checked == true)
                    {
                        device_type = 4;
                        IOSNotification notification = new IOSNotification();
                        notification.title = TBTitle.Text;
                        notification.description = TBDescription.Text;
                        messages = notification.getJsonString();
                    }
                    else
                    {
                        BaiduPushNotification notification = new BaiduPushNotification();
                        notification.title = TBTitle.Text;
                        notification.description = TBDescription.Text;
                        messages = notification.getJsonString();
                    }
                }

                PushOptions pOpts;
                if (RBUnicast.Checked)
                {
                    pOpts = new PushOptions(method, apiKey, TBUserId.Text, TBChannelID.Text, device_type, messages,
                        messageksy, unixTime);
                }
                else if (RBMulticast.Checked)
                {
                    pOpts = new PushOptions(method, apiKey, TBTag.Text, device_type, messages, messageksy, unixTime);
                }
                else
                {
                    pOpts = new PushOptions(method, apiKey, device_type, messages, messageksy, unixTime);
                }

                pOpts.message_type = message_type;
                if (RBIOSPRO.Checked == true)
                {
                    pOpts.deploy_status = 2;
                }
                else if (RBIOSDEV.Checked == true)
                {
                    pOpts.deploy_status = 1;
                }

                string response = Bpush.PushMessage(pOpts);

                TextBoxResponse.Text = response;

            }
            catch (Exception ex)
            {
                TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
            }
        }