极光推送REST API被我给以为是普通API调用了 罪过 记录一下

来源:互联网 发布:js继承 阮一峰 编辑:程序博客网 时间:2024/06/03 01:24
public static string JPush(string msg)        {            string result = "";            HttpWebRequest rq = (HttpWebRequest)HttpWebRequest.Create(JPushURI);            rq.ContentType = "application/json";            rq.Method = "POST";            #region 封装header与消息体            //Header            byte[] buffer = Encoding.Default.GetBytes(AK + ":" + MS);            string base64 = Convert.ToBase64String(buffer);            rq.Headers.Set(HttpRequestHeader.Authorization, base64);            rq.Headers.Set("Authorization", base64);            //消息体            Dictionary<string, object> data = new Dictionary<string, object>();            ArrayList arr = new ArrayList();            arr.Add("android");            data.Add("platform", arr);//推送分平台["android", "ios"]            data.Add("audience", "all");//推送分用户"tag或者alias":["tag","alias"]            Dictionary<string, object> notification = new Dictionary<string, object>();            Dictionary<string, object> androidalert = new Dictionary<string, object>();            androidalert.Add("alert", msg);//分平台传            notification.Add("android", androidalert);            data.Add("notification", notification);            Dictionary<string, object> message = new Dictionary<string, object>();            message.Add("msg_content", "固定消息(非广播)内容体");            data.Add("message", message);            JavaScriptSerializer js = new JavaScriptSerializer();            string json = js.Serialize(data);            #endregion            try            {                using (Stream rqs = rq.GetRequestStream())                {                    byte[] bytejson = Encoding.Default.GetBytes(json);                    rqs.Write(bytejson, 0, bytejson.Length);                    using (HttpWebResponse rp = (HttpWebResponse)rq.GetResponse())                    {                        using (Stream rps = rp.GetResponseStream())                        {                            StreamReader sr = new StreamReader(rps);                            result = sr.ReadToEnd();                        }                    }                }            }            catch (Exception e) { Console.WriteLine("异常:" + e.Message); }            return result;        }

其中:AK MS JPushURI为静态参数需提前声明  序列化需要引用 System.Web.Extensions

书己之过 勉之....