发送WAPPush(C#)

2010-08-28 10:50:35来源:西部e网作者:

找到几个相关代码,都整理出来。

第一个:

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 "发送成功 ^_^";
  }

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的东西,在网上找资料,发现几乎没有,有的也是说的很含糊,最后,尝试了很久,终于解决了,奉献给大家.

(1):使用http://www.codeproject.com/csharp/wappush.asp这里说的方法生成一个16进制的字符串,这个串就是短信内容content.

(2):由于CMPP使用socket发送,因此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协议中,在发送WAPPUSH时候,需要将TP_pID设置为0,将TP_udhi设置为64。

(4):test it!

关键词:C#