public class WapPush { /// /// 同一条Wappush唯一的标识 /// private static Int16 transactionId = 0; private static Int16 TransactionId() { transactionId++; if (transactionId > 127) { transactionId = 0; } return transactionId; } /// /// 第几条 /// private static Int16 currindex = 0; private static Int16 CurrIndex() { currindex++; if (currindex > 127) { currindex = 0; } return currindex; } /// /// WDP /// /// 应该是同一条Wappush唯一的标识(如果拆分成了多条,这几条的refNO应该相同) /// 共几条 /// 第几条 /// private static byte[] WDP(Int16 refNO, Int16 total, Int16 currIndex) { return new byte[]{ (byte)0x0B, 0x05, 0x04, (byte)0x0B, (byte)0x84, 0x23, (byte)0xF0, 0x00, 0x03, (byte)refNO, (byte)total, (byte)currIndex, }; } /// /// 唯一标志 /// /// /// private static byte TransactionId(Int16 Tid) { return (byte)Tid; } /// /// WSP /// private static byte[] WSP { get { return new byte[] { 0x29, 0x06, 0x06, 0x03, 0xAE, 0x81, 0xEA, 0x8D, 0xCA //0x06,0x01,0xAE,(无效) //0x06, 0x04, 0x03, (byte) 0xAE,(byte) 0x81, (byte) 0xEA(无效) //0x06, 0x03, (byte) 0xAE,(byte) 0x81, (byte) 0xEA, (byte) 0x8D, (byte) 0xCA(无效) //0x06, 0x06, 0x03, (byte) 0xAE,(byte) 0x81, (byte) 0xEA, (byte) 0x8D (无效) }; } } /// /// Body开头 /// private static byte[] SiBody { get { return new byte[] { 0x02, 0x05, 0x6A, 0x00, 0x45, 0xC6, 0x0C, 0x03 //0x02,0x05,0x6A,0x00,0x45,0xC6,0x0C,0x03 }; } } /// /// UrlBytes /// /// /// private static byte[] UrlBytes(string Url) { return System.Text.Encoding.ASCII.GetBytes(Url); } /// /// URL结束 /// private static byte[] UrlEnd { get { return new byte[] { 0x00, //0x08, 0x01, 0x03 }; } } private static byte[] created { get { return new byte[] { (byte)0x0A, (byte)0xC3, 0x07, }; } } private static byte[] si_expires { get { return new byte[] { (byte)0x10,//si_expires (byte)0xC3,//时间 0x07,//7个字节 年 月 日 时 分秒 }; } } private static byte[] TimeEnd { get { return new byte[] { 0x01,//> 0x03,//字符串开始,标题 UTF-8编码 }; } } private static byte[] Time { get { DateTime dt = DateTime.Now; string S = dt.Year.ToString() + dt.Month.ToString().PadLeft(2, '0') + dt.Day.ToString().PadLeft(2, '0') + dt.Hour.ToString().PadLeft(2, '0') + dt.Minute.ToString().PadLeft(2, '0') + dt.Second.ToString().PadLeft(2, '0'); byte[] createtime = HexStrToBytes(S); byte[] si_expirestime = createtime; int len = created.Length + createtime.Length + si_expires.Length + si_expirestime.Length + TimeEnd.Length; byte[] time = new byte[len]; int index = 0; created.CopyTo(time, index); index += created.Length; createtime.CopyTo(time, index); index += createtime.Length; si_expires.CopyTo(time, index); index += si_expires.Length; si_expirestime.CopyTo(time, index); index += si_expirestime.Length; TimeEnd.CopyTo(time, index); return time; } } /// /// ContentBytes /// /// /// private static byte[] ContentBytes(string Content) { return System.Text.Encoding.UTF8.GetBytes(Content); } /// /// Content结束 /// private static byte[] ContentEnd { get { return new byte[] { 0x00, 0x01, 0x01 }; } } /// /// SI /// /// /// /// /// private static byte[] SI(string Url, string Content) { byte[] url = UrlBytes(Url); byte[] centent = ContentBytes(Content); byte[] time = Time; int len = SiBody.Length + url.Length + UrlEnd.Length + centent.Length + ContentEnd.Length;//+time.Length ; int index = 0; byte[] si = new byte[len]; SiBody.CopyTo(si, index); index += SiBody.Length; url.CopyTo(si, index); index += url.Length; UrlEnd.CopyTo(si, index); index += UrlEnd.Length; //time.CopyTo(si, index); //index += time.Length; centent.CopyTo(si, index); index += centent.Length; ContentEnd.CopyTo(si, index); return si; } /// /// 得到WappushBytes /// /// /// /// public static IList GetBytes(string Url, string Content) { IList lists = new List(); byte[] si2 = SI(Url, Content); byte[] si = new byte[si2.Length + WSP.Length]; WSP.CopyTo(si, 0); si2.CopyTo(si, WSP.Length); int silen = si.Length; int maxlen = 140 - WDP(0, 0, 0).Length; int index = 0; currindex = 0; Int16 Tid = TransactionId(); Int16 Total = (Int16)(silen / maxlen + 1); if (silen % maxlen == 0) { Total--; } int L_islen = 0;//要截的长度 while (index < silen) { L_islen = silen - index; if (L_islen > maxlen) { L_islen = maxlen; } byte[] sis = new byte[L_islen]; Array.Copy(si, index, sis, 0, L_islen); index += L_islen; byte[] header = WDP(Tid, Total, CurrIndex()); int len = header.Length + sis.Length; int I = 0; byte[] list = new byte[len]; header.CopyTo(list, I); I += header.Length; sis.CopyTo(list, I); I += si.Length; lists.Add(list); //} } return lists; } #region 朋友代码 private static byte[] Getbytes(string SendUrl, string messageContent) { int iLen, Msg_Length; byte[] szWapPushHeader1 = {0x0B, 0x05, 0x04, 0x0B, 0x84, 0x23, 0xF0, 0x00, 0x03, 0x09, 0x01, 0x01}; byte[] szWapPushHeader2 = { 0x29, 0x06, 0x06, 0x03, 0xAE, 0x81, 0xEA, 0x8D, 0xCA }; byte[] szWapPushIndicator = { 0x02, 0x05, 0x6A, 0x00, 0x45, 0xC6, 0x0C, 0x03 }; byte[] szWapPushDisplayTextHeader = { 0x00, 0x01, 0x03 }; byte[] szEndOfWapPush = { 0x00, 0x01, 0x01 }; byte[] szWapPushUrl = { 0x77, 0x61, 0x70, 0x2E, 0x73, 0x69, 0x6e, 0x61, 0x2e, 0x63, 0x6f, 0x6d }; //Encoding.UTF8.GetBytes(SendUrl);// byte[] szHex = new byte[1024]; byte[] szMsg = { 0xE5, 0xA5, 0xBD, 0xE5, 0xA5, 0xBD, 0xE5, 0xAD, 0xA6, 0xE4, 0xB9, 0xA0 }; //Encoding.UTF8.GetBytes(messageContent);// iLen = 0; Array.Copy(szWapPushHeader1, 0, szHex, iLen, szWapPushHeader1.Length); iLen += szWapPushHeader1.Length; Array.Copy(szWapPushHeader2, 0, szHex, iLen, szWapPushHeader2.Length); iLen += szWapPushHeader2.Length; Array.Copy(szWapPushIndicator, 0, szHex, iLen, szWapPushIndicator.Length); iLen += szWapPushIndicator.Length; Array.Copy(szWapPushUrl, 0, szHex, iLen, szWapPushUrl.Length); iLen += szWapPushUrl.Length; Array.Copy(szWapPushDisplayTextHeader, 0, szHex, iLen, szWapPushDisplayTextHeader.Length); iLen += szWapPushDisplayTextHeader.Length; Array.Copy(szMsg, 0, szHex, iLen, szMsg.Length); iLen += szMsg.Length; Array.Copy(szEndOfWapPush, 0, szHex, iLen, szEndOfWapPush.Length); iLen += szEndOfWapPush.Length; Msg_Length = iLen; byte[] temp_mess = new byte[Msg_Length]; Array.Copy(szHex, 0, temp_mess, 0, Msg_Length); return temp_mess; } #endregion }
使用例子
IList lists = WapPush.GetBytes(Url, Title); for (int J = 0; J < lists.Count; J++) { TCMPP_SUBMIT Submit = CMPP_SUBMIT; //Submit.Pk_total = 1;//(byte)(lists.Count); //Submit.Pk_number = 1;//(byte)(J + 1); Submit.Pk_total = (byte)(lists.Count); Submit.Pk_number = (byte)(J + 1); if (J != 0) { Sequence_ID = CMPP_GetLSH(); } Send_CMPP_SUBMIT_Wappush(Sequence_ID, lists[J], Submit); }