發(fā)送WAPPush(C#)

2010-08-28 10:50:35來源:西部e網(wǎng)作者:

找到幾個相關(guān)代碼,都整理出來。

第一個:

public static string WapPush(string Host,int Port,string Data,int Timeout)
  {
   TcpClient tc = new TcpClient();
   tc.Connect(Host, Port);
   NetworkStream ns = tc.GetStream();
   byte[] bytes = null;
   if (ns.CanWrite)
   {
    int i = 0;
    byte[] buffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Data);
    ns.Write(buffer,0,buffer.Length);   
   
   }  
   return "發(fā)送成功 ^_^";
  }

example:

WapPush("主機IP","端口號","手機號"+"\t"+"鏈接url"+"\t"+描述+"\t1\n",5)

還有一個:

CMPP3.0 , C# 源碼
submit.TP_pID=0
submit.TP_udhi=1
submit.Msg_Fmt=0x04

WAPPUSH wap=new WAPPUSH();
sumbit.Msg_content=wap.toBytes(msg,url);

  public class WAPPUSH
        {
            public WAPPUSH()
            {
            }
            //第一部分
            static private readonly byte[] WapPushHeader1 = new byte[]
{
  0x0B, 0x05, 0x04, 0x0B, 0x84, 0x23, 0xF0, 0x00, 0x03, 0x03, 0x01, 0x01
};
            //第二部分
            static private readonly byte[] WapPushHeader2 = new byte[]
{
  0x29, 0x06, 0x06, 0x03, 0xAE, 0x81, 0xEA, 0x8D, 0xCA
};

            //第三部分
            static private readonly byte[] WapPushIndicator = new byte[]
{
  0x02, 0x05, 0x6A, 0x00, 0x45, 0xC6, 0x0C, 0x03
};
            //第四部分:URL去掉http://后的UTF8編碼
            //第五部分
            static private readonly byte[] WapPushDisplayTextHeader = new byte[]
   {
    0x00, 0x01, 0x03,
   };
            //第六部分:消息文字的UTF8編碼
            //第七部分:
            static private readonly byte[] EndOfWapPush = new byte[]
   {
  0x00, 0x01, 0x01,
   };
            public byte[] toBytes(string WAP_Msg, string WAP_URL)
            {
                byte[] submitData = new byte[400];
                int index = 0;
                WapPushHeader1.CopyTo(submitData, index);
                index += WapPushHeader1.Length;

                WapPushHeader2.CopyTo(submitData, index);
                index += WapPushHeader2.Length;
                              
                WapPushIndicator.CopyTo(submitData, index);
                index += WapPushIndicator.Length;

                byte[] url = Encoding.UTF8.GetBytes(WAP_URL);
                url.CopyTo(submitData, index);
                index += url.Length;

                WapPushDisplayTextHeader.CopyTo(submitData, index);
                index += WapPushDisplayTextHeader.Length;

                byte[] msg2 = Encoding.UTF8.GetBytes(WAP_Msg);
                msg2.CopyTo(submitData, index);
                index += msg2.Length;
                              
                EndOfWapPush.CopyTo(submitData, index);
                index += 3;

                byte[] reVal = new byte[index];
                for (int i = 0; i < reVal.Length; i++)
                {
                    reVal = submitData;
                }

                return (reVal);
            }
           
        }

還有:

前一段,給移動做一個wappush的東西,在網(wǎng)上找資料,發(fā)現(xiàn)幾乎沒有,有的也是說的很含糊,最后,嘗試了很久,終于解決了,奉獻給大家.

(1):使用http://www.codeproject.com/csharp/wappush.asp這里說的方法生成一個16進制的字符串,這個串就是短信內(nèi)容content.

(2):由于CMPP使用socket發(fā)送,因此content需要進行二進制編碼,那么如果你采用c#語言,請使用下面的方法,其他語言類同。

public static byte[] hex2Ascii(string s)

         {

 

              int len = s.Length;

 

              byte[] temp = new byte[len/2];

              int j = 0;

              for(int i=0;i<len;i++)

              {

                   string s2 = s.Substring(i,2);

                   string s21 = s2.Substring(0,1);

                   string s22 = s2.Substring(1,1);

                   temp[j]=(byte)(str2byte(s21) * 16 + str2byte(s22));

                   i++;

                   j++;

              }

              return temp;

         }

         public static int str2byte(string ch)

         {

              string aa = ch.ToLower();

              if(aa.Equals("a")) return 10;

              if(aa.Equals("b")) return 11;

              if(aa.Equals("c")) return 12;

              if(aa.Equals("d")) return 13;

              if(aa.Equals("e")) return 14;

              if(aa.Equals("f")) return 15;

              return Convert.ToInt32(aa);

 

         }

(3):在CMPP協(xié)議中,在發(fā)送WAPPUSH時候,需要將TP_pID設置為0,將TP_udhi設置為64。

(4):test it!

關(guān)鍵詞:C#