激光推送(ios,安卓)

时间:2023-03-08 18:34:26

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Compression; using System.IO; using System.Net;

namespace demo {     class MD5     {         public static string MD5Encrypt(string strSource)         {

return MD5Encrypt(strSource, 32);

}

/// <summary>

/// </summary>

/// <param name="strSource">待加密字串</param>

/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>

/// <returns>加密后的字串</returns>

public static string MD5Encrypt(string strSource, int length)         {

byte[] bytes = Encoding.ASCII.GetBytes(strSource);

byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);

StringBuilder sb = new StringBuilder();

switch (length)             {

case 16:

for (int i = 4; i < 12; i++)

sb.Append(hashValue[i].ToString("x2"));

break;

case 32:

for (int i = 0; i < 16; i++)                     {

sb.Append(hashValue[i].ToString("x2"));

}

break;

default:

for (int i = 0; i < hashValue.Length; i++)                     {

sb.Append(hashValue[i].ToString("x2"));

}

break;

}

return sb.ToString();

}

public static string doSend()         {

IDictionary<string, string> parameters = new Dictionary<string, string>();

string html = string.Empty;

int sendno = 1;

string receiverValue = "eric";//这个是一个别名

int receiverType = 4;

string appkeys = "51b45123b7313212ba35123dcc7e303123257eb";

String input = sendno.ToString() + receiverType + "" + "5232135e230314a412321312bb2fb12133d27c845";

string verificationCode = MD5Encrypt(input);

string content = "{\"n_content\":\"" + receiverValue + "\",\"n_builder_id\":\"1\"}"; //发送的内容

string loginUrl = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg";

parameters.Add("sendno", sendno.ToString());

parameters.Add("app_key", appkeys);

parameters.Add("receiver_type", receiverType.ToString());

parameters.Add("verification_code", verificationCode);   //MD5

parameters.Add("msg_type", "1");

parameters.Add("msg_content", content);        //内容

parameters.Add("platform", "android,ios");

HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null);

if (response != null)             {

// 得到返回的数据流

Stream receiveStream = response.GetResponseStream();

// 如果有压缩,则进行解压

if (response.ContentEncoding.ToLower().Contains("gzip"))                 {

receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);

}

// 得到返回的字符串

html = new StreamReader(receiveStream).ReadToEnd();

}

return html;

}

} }

byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());

using (Stream stream = request.GetRequestStream())             {

stream.Write(data, 0, data.Length);

}

}

return request.GetResponse() as HttpWebResponse;

}

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)     {

return true; //总是接受

}

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.IO.Compression;

namespace demo {     class Program     {         public static string MD5Encrypt(string strSource)         {

return MD5Encrypt(strSource, 32);

}         public static string MD5Encrypt(string strSource, int length)         {

byte[] bytes = Encoding.ASCII.GetBytes(strSource);

byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);

StringBuilder sb = new StringBuilder();

switch (length)             {

case 16:

for (int i = 4; i < 12; i++)

sb.Append(hashValue[i].ToString("x2"));

break;

case 32:

for (int i = 0; i < 16; i++)                     {

sb.Append(hashValue[i].ToString("x2"));

}

break;

default:

for (int i = 0; i < hashValue.Length; i++)                     {

sb.Append(hashValue[i].ToString("x2"));

}

break;

}

return sb.ToString();

}         public static string doSend()         {

IDictionary<string, string> parameters = new Dictionary<string, string>();

string html = string.Empty;

int sendno = 1;

string receiverValue = "863094010028545";//这个是一个别名   如果使用别名需要加参数 parameters.Add("receiver_value", receiverValue.ToString());  否则去掉这行代码

int receiverType = 3;//3是使用别名,4是使用广播

string appkeys = "6749f40904629f928a5c28e4";             string appkeysCode = "ed55187688f7ba184d34cb97";//极光推送portal 上分配的 appKey 的验证串(masterSecret)             String input = sendno.ToString() + receiverType + receiverValue + appkeysCode;

string verificationCode = MD5Encrypt(input);             string strcontent = "测试";             string content = "{\"n_content\":\"" + strcontent + "\",\"n_builder_id\":\"1\"}"; //发送的内容

string loginUrl = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg";

parameters.Add("sendno", sendno.ToString());

parameters.Add("app_key", appkeys);

parameters.Add("receiver_type", receiverType.ToString());             parameters.Add("receiver_value", receiverValue.ToString());             parameters.Add("verification_code", verificationCode);   //MD5

parameters.Add("msg_type", "1");

parameters.Add("msg_content", content);        //内容

parameters.Add("platform", "android,ios");

HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null);

if (response != null)             {

// 得到返回的数据流

Stream receiveStream = response.GetResponseStream();

// 如果有压缩,则进行解压

if (response.ContentEncoding.ToLower().Contains("gzip"))                 {

receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress);

}

// 得到返回的字符串

html = new StreamReader(receiveStream).ReadToEnd();

}

Console.WriteLine("{0}", html);             return html;

}         static void Main(string[] args)         {             doSend();             Console.ReadKey();         }     } }

}