JPUSH.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Web;
  9. namespace Ant.Common
  10. {
  11. /// <summary>
  12. /// 激光推送
  13. /// </summary>
  14. public class JPUSH
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="aliass"></param>
  20. /// <param name="msg"></param>
  21. public static void sendRequest(string aliass, string msg)
  22. {
  23. String app_key = ConfigurationManager.AppSettings["JPushKey"];
  24. String master_secret = ConfigurationManager.AppSettings["JPushSecret"];
  25. byte[] bytes = Encoding.Default.GetBytes((app_key + ":" + master_secret));
  26. string auth = Convert.ToBase64String(bytes);
  27. String url = "https://api.jpush.cn/v3/push";
  28. string pushmsg = "{\"platform\":\"all\",\"audience\":{\"alias\":" + aliass + "},\"notification\":{\"alert\":" + msg + ",\"android\":{},\"ios\":{\"sound\":\"sound.caf\",\"badge\":1}},\"options\":{\"apns_production\":false}}";
  29. HttpWebRequest myReq = null;
  30. HttpWebResponse response = null;
  31. try
  32. {
  33. myReq = (HttpWebRequest)WebRequest.Create(url);
  34. myReq.Method = "POST";
  35. myReq.ContentType = "application/json";
  36. myReq.Headers.Add("Authorization", "Basic " + auth);
  37. byte[] bs = UTF8Encoding.UTF8.GetBytes(pushmsg);
  38. myReq.ContentLength = bs.Length;
  39. using (Stream reqStream = myReq.GetRequestStream())
  40. {
  41. reqStream.Write(bs, 0, bs.Length);
  42. reqStream.Close();
  43. }
  44. myReq.GetResponse();
  45. //string res = "";
  46. //response = (HttpWebResponse)myReq.GetResponse();
  47. //HttpStatusCode statusCode = response.StatusCode;
  48. //if (Equals(response.StatusCode, HttpStatusCode.OK))
  49. //{
  50. // using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
  51. // {
  52. // res = reader.ReadToEnd();
  53. // }
  54. //}
  55. //res = res + "";
  56. }
  57. catch{}
  58. finally
  59. {
  60. if (response != null)
  61. {
  62. response.Close();
  63. }
  64. if (myReq != null)
  65. {
  66. myReq.Abort();
  67. }
  68. }
  69. }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="aliass"></param>
  74. /// <param name="msg"></param>
  75. public static void sendRequest_app(string aliass, string msg)
  76. {
  77. String app_key = ConfigurationManager.AppSettings["JpushKey_us"];
  78. String master_secret = ConfigurationManager.AppSettings["JpushSecret_us"];
  79. byte[] bytes = Encoding.Default.GetBytes((app_key + ":" + master_secret));
  80. string auth = Convert.ToBase64String(bytes);
  81. String url = "https://api.jpush.cn/v3/push";
  82. string pushmsg = "{\"platform\":\"all\",\"audience\":{\"alias\":" + aliass + "},\"notification\":{\"alert\":" + msg + ",\"android\":{},\"ios\":{\"sound\":\"sound.caf\",\"badge\":1}},\"options\":{\"apns_production\":false}}";
  83. HttpWebRequest myReq = null;
  84. HttpWebResponse response = null;
  85. try
  86. {
  87. myReq = (HttpWebRequest)WebRequest.Create(url);
  88. myReq.Method = "POST";
  89. myReq.ContentType = "application/json";
  90. myReq.Headers.Add("Authorization", "Basic " + auth);
  91. byte[] bs = UTF8Encoding.UTF8.GetBytes(pushmsg);
  92. myReq.ContentLength = bs.Length;
  93. using (Stream reqStream = myReq.GetRequestStream())
  94. {
  95. reqStream.Write(bs, 0, bs.Length);
  96. reqStream.Close();
  97. }
  98. myReq.GetResponse();
  99. //string res = "";
  100. //response = (HttpWebResponse)myReq.GetResponse();
  101. //HttpStatusCode statusCode = response.StatusCode;
  102. //if (Equals(response.StatusCode, HttpStatusCode.OK))
  103. //{
  104. // using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
  105. // {
  106. // res = reader.ReadToEnd();
  107. // }
  108. //}
  109. //res = res + "";
  110. }
  111. catch { }
  112. finally
  113. {
  114. if (response != null)
  115. {
  116. response.Close();
  117. }
  118. if (myReq != null)
  119. {
  120. myReq.Abort();
  121. }
  122. }
  123. }
  124. //Common.JPUSH.sendRequest(("[\"" + note.CM_USERID + "\"]"), ("\"用户#" + username + "#回复了您的帖子\""));
  125. //aliass = "[\"18888888888\",\"17777777777\"]";
  126. //msg = "\"晚上带你抢银行\"";
  127. //response = (HttpWebResponse)myReq.GetResponse();
  128. //HttpStatusCode statusCode = response.StatusCode;
  129. //if (Equals(response.StatusCode, HttpStatusCode.OK))
  130. //{
  131. // using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
  132. // {
  133. // res = reader.ReadToEnd();
  134. // }
  135. //}
  136. }
  137. }