系列WAPPUSH代码-更新版本

package com.wap;

import java.io.IOException;
import java.util.Calendar;
import java.util.Vector;

import com.tools.Utf8Util;

import sun.misc.BASE64Decoder;

public class WapPushEncoder {
 
 
 public static String getBASE64(String s) {
  if(s == null)
   return null;
  return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
 }
 
 
 public static String getFromBASE64(String s) {
  if(s == null)
   return null;
  BASE64Decoder decoder = new BASE64Decoder();
  try {
   byte[] b = decoder.decodeBuffer(s);
   System.out.print(new String(b));
   return new String(b);
  }
  catch(Exception e) {
   return null;
  }
  
 }
 
 
 /**
  * @param content
  * @param url
  * @return
  * NOKIA 6600可以通过,其余不是很顺利;多普大也收到
  * 李伟版本;
  */
 public  byte[] format(String content, String url) {
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
   
//   System.out.println(this.byteToHexStr(bytContent));
//   System.out.println(this.byteToHexStr(this.chString2UTF8_byte(content)));
//   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  }
  
  int iContentLen = bytContent.length;
  int iURLLen = bytURL.length;
  
  byte[] bytMsg = new byte[36 + iContentLen + iURLLen];
  
  int i = 0;
  bytMsg[i++] = (byte)0x06;
  bytMsg[i++] = (byte)0x05;
  bytMsg[i++] = (byte)0x04;
  bytMsg[i++] = (byte)0x0b;
  bytMsg[i++] = (byte)0x84;
  bytMsg[i++] = (byte)0x23;
  bytMsg[i++] = (byte)0xf0;
  
  bytMsg[i++] = (byte)0x72;
  bytMsg[i++] = (byte)0x06;
  bytMsg[i++] = (byte)0x0a;
  bytMsg[i++] = (byte)0x03;
  bytMsg[i++] = (byte)0xae;
  bytMsg[i++] = (byte)0x81;
  bytMsg[i++] = (byte)0xea;
  bytMsg[i++] = (byte)0xaf;
  bytMsg[i++] = (byte)0x82;
  bytMsg[i++] = (byte)0x8d;
  bytMsg[i++] = (byte)0xae; // 18
  
  int ipos = i;
  bytMsg[i++] = (byte)159; //159+iURLLen
  
  bytMsg[i++] = (byte)0x87;
  bytMsg[i++] = (byte)0x01;
  bytMsg[i++] = (byte)0x05;
  bytMsg[i++] = (byte)0x6a;
  bytMsg[i++] = (byte)(iContentLen + 1);
  
  System.arraycopy(bytContent, 0, bytMsg, i, iContentLen);
  i += iContentLen;
  bytMsg[i++] = (byte)0x00;
  bytMsg[i++] = (byte)0x45;
  bytMsg[i++] = (byte)0xc6;
  bytMsg[i++] = (byte)0x0c;
  bytMsg[i++] = (byte)0x03;
  
  System.arraycopy(bytURL, 0, bytMsg, i, iURLLen);
  i += iURLLen;
  bytMsg[ipos] += (byte)iURLLen;
  bytMsg[i++] = (byte)0x00;
  bytMsg[i++] = (byte)0x08;
  bytMsg[i++] = (byte)0x01;
  bytMsg[i++] = (byte)0x83;
  bytMsg[i++] = (byte)0x00;
  bytMsg[i++] = (byte)0x01;
  bytMsg[i++] = (byte)0x01;
  
  // System.out.println("BYTE LENGTH : " + bytMsg.length);
  
  return bytMsg;
 }
 
 public  String byteToHexStr(byte buf[]) {
  StringBuffer sb = new StringBuffer(2 * buf.length);
  for(int i = 0; i < buf.length; i++) {
   int h = (buf[ i ] & 0xf0) >> 4;
  int l = (buf[ i ] & 0x0f);
  sb.append(new Character((char)((h > 9) ? 'a' + h - 10 : '0' + h)));
  sb.append(new Character((char)((l > 9) ? 'a' + l - 10 : '0' + l)));
  }
  return sb.toString().toUpperCase();
 }
 
 public  String intToHexStr(int i) {
  if(i>255)
   return "00";
  StringBuffer sb = new StringBuffer(2);
  int h=0;
  int l=0;
  h=i/16;
//  System.out.println(h);
  l=i%16;
//  System.out.println(l);
  
  sb.append(new Character((char)((h > 9) ? 'a' + h - 10 : '0' + h)));
  sb.append(new Character((char)((l > 9) ? 'a' + l - 10 : '0' + l)));
  
  System.out.println(sb.toString().toUpperCase());
  
  return sb.toString().toUpperCase();
 }
 
 private byte[] chString2UTF8_byte(String str) {
  byte[] result = null;
  try{
   str = new String(str.getBytes(),"GB2312");
   result = str.getBytes("UTF-8");
  }catch (Exception e){
   e.printStackTrace();
  }
  
  return result;
 }
 

 
 /**
  * @param content
  * @param url
  * @return
  * 没有长度控制。
  */
 public byte[] format5(String content,String url){
  //第一部分
  byte[] WapPushUDH = new byte[]
                               {
    0x06, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0
    
                               };
  //第二部分
  byte[] WapPushPUD = new byte[]
                               {
    0x25,0x06,0x01,(byte) 0xae
    
                               };
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6, 0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //>
//  *pszPos++ = 0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
  //第七部分:
  
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //</indication>"
//  *pszPos++ = 0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
//  String wapPushHeader1="0B05040B8423F00003030101";
//  String wapPushHeader2="29060603AE81EA8DCA";
//  String wapPushIndicator="02056A0045C60C03";
////  第四部分:URL去掉http://后的UTF8编码
//  String wapPushDisplayTextHeader="000103";
////  第六部分:消息文字的UTF8编码
//  String wapPushEnd="000101";
  
  
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
//  int totalLen=nWapPushUDHLen+nWapPushPUDLen+nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
//  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128); 不能使用
  
  byte[] bytMsg = new byte[totalLen];
  
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
  
 }
 
 /**
  * @param content
  * @param url
  * @return
  *  包头c:
  UDH: 06 05 04 0B 84 23 F0
  PUD: 81 06 06 03 AE 81 EA 8D 00
  有长度控制
 
  */
 public byte[] format1(String content,String url){
  byte[] WapPushUDH = new byte[]
                               {
    0x06,0x05,0x04,0x0B,(byte) 0x84,0x23,(byte) 0xF0
                               };
  
  
  byte[] WapPushPUD = new byte[]
                               {
    
    (byte) 0x81,0x06,0x06,0x03,(byte) 0xAE,(byte) 0x81,(byte) 0xEA,(byte) 0x8D,0x00
                               };
  
//  *pszPos++ = 0x02;;
//  *pszPos++ = 0x05; //-//WAPFORUM//DTD SI 1.0//EN
//  *pszPos++ = 0x6A; //UTF-8
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x45; //<si>
//  *pszPos++ = 0xC6; //<indication
//  *pszPos++ = 0x08; //<action=signal-high>
//  *pszPos++ = 0x0C; //href="http://"
//  *pszPos++ = 0x03; //字符串开始
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //>
//  *pszPos++ = 0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
//  //第七部分:
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //</indication>"
//  *pszPos++ = 0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
 }
 
 /**
  * @param content
  * @param url
  * @return
  *  包头a:
  UDH: 06 05 04 0B 84 23 F0
  PUD: 01 06 03 AE 8D C4
  WapPushPUD,最后一位,长度控制,有没有,NOKIA SR618也可以接受,有也可以
  */
 public byte[] format2(String content,String url){
  byte[] WapPushUDH = new byte[]
                               {
    0x06,0x05 ,0x04 ,0x0B ,(byte) 0x84 ,0x23 ,(byte) 0xF0
                               };
  //第二部分
  byte[] WapPushPUD = new byte[]
                               {
    0x01 ,0x06 ,0x03 ,(byte) 0xAE ,(byte) 0x8D ,(byte) 0xC4
                               };
  
//  *pszPos++ = 0x02;;
//  *pszPos++ = 0x05; //-//WAPFORUM//DTD SI 1.0//EN
//  *pszPos++ = 0x6A; //UTF-8
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x45; //<si>
//  *pszPos++ = 0xC6; //<indication
//  *pszPos++ = 0x08; //<action=signal-high>
//  *pszPos++ = 0x0C; //href="http://"
//  *pszPos++ = 0x03; //字符串开始
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //>
//  *pszPos++ = 0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
//  //第七部分:
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //</indication>"
//  *pszPos++ = 0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);//没有的这行,NOKIA也可以成功的。 有这行也可以。
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
 }
 
 /**
  * @param content
  * @param url
  * @return
  *  包头b:
  UDH: 0B 05 04 0B 84 23 F0 00 03 03 01 01
  PUD: 29 06 06 03 AE 81 EA 8D CA
  //WapPushPUD,最后一位,长度控制,有没有,NOKIA SR618也可以接受,有也可以。
  */
 public byte[] format3(String content,String url){
  

//  第一条:

//  0B                  WAPPUSH头部的总长度,不包括本字节
//  05                  IE=16bit port addressing
//  04                  lenght of ports
//  0B84                dest. port
//  23F0                source port
//  00                  表示是Concatenated Short Messages
//  03                  udh头长度
//  B3                  reference number
//  02                  02是指共有两条短信
//  01                  01是指这是第一条
//  
//  接除UDH外的部分
//  ...
//  第二条:
//  0B                  WAPPUSH头部的总长度 ,不包括本字节
//  05040B8423F0        表示接下来是一个WAPPUSH     6字节
//  00                  表示是Concatenated Short Messages
//  03                  udh头长度
//  B3                  reference number
//  02                  02是指共有两条短信
//  02                  02是指这是第2条
//  
//  接余下部分
  
  byte[] WapPushUDH = new byte[]
                               {
//    0x0B ,0x05  ,0x04  ,0x0B  ,(byte) 0x84  ,0x23  ,(byte) 0xF0  ,0x00  ,0xB3  ,0x03  ,0x01  ,0x01
    0x0B ,0x05  ,0x04  ,0x0B  ,(byte) 0x84  ,0x23  ,(byte) 0xF0  ,0x00  ,0x03  ,0x03  ,0x01  ,0x01
                               };
  //第二部分
  byte[] WapPushPUD = new byte[]
                               {
//    0xB3,0x06,0x01,0xAE
    0x29  ,0x06  ,0x06  ,0x03  ,(byte) 0xAE  ,(byte) 0x81  ,(byte) 0xEA  ,(byte) 0x8D  ,(byte) 0xCA
                               };
  
//  *pszPos++ = 0x02;;
//  *pszPos++ = 0x05; //-//WAPFORUM//DTD SI 1.0//EN
//  *pszPos++ = 0x6A; //UTF-8
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x45; //<si>
//  *pszPos++ = 0xC6; //<indication
//  *pszPos++ = 0x08; //<action=signal-high>
//  *pszPos++ = 0x0C; //href="http://"
//  *pszPos++ = 0x03; //字符串开始
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
//    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x0C, 0x03
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //>
//  *pszPos++ = 0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
//  //第七部分:
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //</indication>"
//  *pszPos++ = 0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128); //没有这行,NOKIA也可以接受,有也可以。
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
 }
 

 
 /**
  * @param content
  * @param url
  * @return
  * 有长度控制。WapPushPUD 最后一位
  */
 public byte[] format6(String content,String url){
  
  
//  $udh .= chr(0x06); //User Data Header Length (6 bytes)
//  $udh .= chr(0x05); //$udh Item Element id (Port Numbers)
//  $udh .= chr(0x04); //$udh IE length (4 bytes)
//  $udh .= chr(0x0B);
//  $udh .= chr(0x84); //destination port number
//  $udh .= chr(0x23);
//  $udh .= chr(0xF0); //origin port number 
  
  
  
  byte[] WapPushUDH = new byte[]
                               {
    0x06,0x05 ,0x04 ,0x0B ,(byte) 0x84 ,0x23 ,(byte) 0xF0
                               };
//  $pud .= chr(0x72); //transaction id (connectionless WSP)
//  $pud .= chr(0x06); //'pdu type (06=push)
//  $pud .= chr(0x0a); //Headers len
//  $pud .= chr(0x03);
//  $pud .= chr(0xAE);
//  $pud .= chr(0x81);
//  $pud .= chr(0xEA); //content type: application/vnd.wap.sic; charset=utf-8
//  $pud .= chr(0xaf);
//  $pud .= chr(0x82);
//  $pud .= chr(0x8D); //content-length WBXML长度;
  byte[] WapPushPUD = new byte[]
                               {
    0x72 ,0x06 ,0x0a ,0x03 ,(byte) 0xAE ,(byte) 0x81,(byte) 0xEA,(byte) 0xaf,(byte) 0x82,(byte) 0x8D
                               };
  
  
//  $body .= chr(0x80);
//  $body .= chr(0xb4);
//  $body .= chr(0x84);
//  // Begin Pos
//  $body .= chr(0x02);
//  $body .= chr(0x05); //-//WAPFORUM//DTD SI 1.0//EN
//  $body .= chr(0x6A); //UTF-8
//  // $body .= chr(0x03); //字符串开始
//  // $body .= iconv( "GBK", "UTF-8", $subject ); //显示给用户的内容,用utf-8编码。utf-8编码,英文字符直接用ascii码;中文如果unicode是(二进制)
//  $body .= chr(0x00); //字符串结束
//  $body .= chr(0x45); //<si>
//  $body .= chr(0xC6); //<indication
//  $body .= chr(0x0C); //href="http://
//  $body .= chr(0x03); //字符串开始
  
  byte[] WapPushIndicator = new byte[]
                                     {
    (byte) 0x80, (byte) 0xb4, (byte) 0x84, 0x02, 0x05, (byte) 0x6A,0x00,0x45, (byte) 0xC6,0x0C,0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
//  $body .= chr(0x00); //字符串结束
//  $body .= chr(0x08);   // action=signal-high
//  $body .= chr(0x01); ;  // END( of indication attribute list)
//  
//  $body .= chr(0x03); //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x08,0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
//  $body .= chr(0x00); //字符串结束
//  $body .= chr(0x01); ;  // END( of indication attribute list)
//  $body .= chr(0x01); ;  // END( of indication attribute list)
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
//   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
////   System.out.println(this.byteToHexStr(bytContent));
//   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
  
  
 }
 
 /**
  * @param content
  * @param url
  * @return 老赵版本,URL和CONTENT互换会有不同的效果;
  * 没有长度控制。

                                  */
 public byte[] format4(String content, String url){
  
  
  byte[] WapPushUDH = new byte[]
                               {
    0x06, 0x05, 0x04, 0x0b, (byte) 0x84, 0x23,(byte) 0xf0
                               };
  
  byte[] WapPushPUD = new byte[]
                               {
    0x25, 0x06, 0x01, (byte) 0xae
                               };
  
  
  byte[] WapPushIndicator = new byte[]
                                     {
////    比老赵版本是01,部分代码是02
//    0x02, 0x05, 0x6a, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03 //比老赵帮本多了一个,在这里;//不成功;
    0x01, 0x05, 0x6a, 0x00, 0x45, (byte) 0xC6, 0x0C, 0x03 //老赵版本;不成功
//    0x02, 0x05, 0x6a, 0x00, 0x45, (byte) 0xC6,0x0C, 0x03    //把01改成02,和FORMAT5一样了,FORMAT5可以成功收到。
                                     };
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
//   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
//   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
   bytURL = urlUTF8.getBytes();
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
//  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
 }
 
 /**
  * @param content
  * @param url
  * @return
  * 网上的代码,WBXML太长。
  */
 public byte[] format7(String content,String url){
  
  Calendar calendar = Calendar.getInstance();
  String currentTime = getTimeString(calendar);
//  System.out.println(currentTime);
  calendar.add(calendar.MONTH, 1); //一个月以后的时间
  String nextTime = getTimeString(calendar);
  byte[] startTime= getTimeBytes(currentTime);
  byte[] endTime = getTimeBytes(nextTime);
//  System.out.println(this.byteToHexStr(endTime));
//  System.out.println(this.byteToHexStr(startTime));
  
  
//  udh = udh + "06";
//  udh = udh + "05";
//  udh = udh + "04";
//  udh = udh + "0B";
//  udh = udh + "84";
//  udh = udh + "23";
//  udh = udh + "F0";
  
  byte[] WapPushUDH = new byte[]
                               {
    0x06,0x05,0x04,0x0B,(byte) 0x84,0x23,(byte) 0xF0
                               };
//  pud = pud + "81";
//  pud = pud + "06";
//  pud = pud + "08";
//  pud = pud + "03";
//  pud = pud + "AE";
//  pud = pud + "81";
//  pud = pud + "EA";
//  pud = pud + "b4";
//  pud = pud + "84";
//  pud = pud + "8D";
  
  byte[] WapPushPUD = new byte[]
                               {
    (byte) 0x81,0x06,0x08,0x03,(byte) 0xAE,(byte) 0x81,(byte) 0xEA,(byte) 0xb4,(byte) 0x84,(byte) 0x8D,0x00
    
                               };
  
  
  
//  body = body + "02";
//  body = body + "05";
//  body = body + "6A";
//  body = body + "00";
//  body = body + "45";
//  body = body + "C6";
//  body = body + "08";
//  body = body + "0C";
//  body = body + "03";
  
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  body = body + "00";
//  body = body + "0A";
//  body = body + "C3";////时间字
//  body = body + "07";////时间字节数
  byte[] WapPushDisplayTextHeader1 = new byte[]
                                              {
    0x00, 0x0a, (byte) 0xc3,//0x07
                                              };
  
//  body = body + startTime;
  
//  body = body + "10";
//  body = body + "C3";//时间字
//  body = body + "07";////时间字节数
  byte[] WapPushDisplayTextHeader2 = new byte[]
                                              {
    0x10, (byte) 0xc3,// 0x07
                                              };
  
//  body = body + endTime;
//  body = body + "01";
//  body = body + "03";
//  
  byte[] WapPushDisplayTextHeader3 = new byte[]
                                              {
    0x01, 0x03
                                              };
  //第六部分:消息文字的UTF8编码
//  //第七部分:

  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeader1Len=WapPushDisplayTextHeader1.length;
  int nStartTimeLen=startTime.length;
  
  int nPushDisplayTextHeader2Len=WapPushDisplayTextHeader2.length;
  int nEndTimeLen=endTime.length;
  
  int nPushDisplayTextHeader3Len=WapPushDisplayTextHeader3.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeader1Len+nStartTimeLen+nPushDisplayTextHeader2Len+nEndTimeLen+nPushDisplayTextHeader3Len+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader1, 0, bytMsg, i, nPushDisplayTextHeader1Len);
  i+=nPushDisplayTextHeader1Len;
  
  System.arraycopy(startTime, 0, bytMsg, i, nStartTimeLen);
  i+=nStartTimeLen;
  
  System.arraycopy(WapPushDisplayTextHeader2, 0, bytMsg, i, nPushDisplayTextHeader2Len);
  i+=nPushDisplayTextHeader2Len;
  
  System.arraycopy(endTime, 0, bytMsg, i, nEndTimeLen);
  i+=nEndTimeLen;
  
  System.arraycopy(WapPushDisplayTextHeader3, 0, bytMsg, i, nPushDisplayTextHeader3Len);
  i+=nPushDisplayTextHeader3Len;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
 }
 
 
 /**
  * @param content
  * @param url
  * @return
  * 有长度控制。WapPushPUD最后一位
  */
 public byte[] format8(String content,String url){
  //第一部分
  byte[] WapPushUDH = new byte[]
                               {
    
    0x0B, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0, 0x00, 0x03, 0x03, 0x01, 0x01
                               };
  //第二部分
  byte[] WapPushPUD = new byte[]
                               {
    
    0x29, 0x06, 0x06, 0x03, (byte) 0xAE, (byte) 0x81, (byte) 0xEA, (byte) 0x8D, (byte) 0xCA
                               };
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6, 0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  0x00; //字符串结束
//  0x01; //>
//  0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
  //第七部分:
  
//  0x00; //字符串结束
//  0x01; //</indication>"
//  0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
//  int totalLen=nWapPushUDHLen+nWapPushPUDLen+nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  byte[] bytMsg = new byte[totalLen];
  
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
  
 }
 
 /**
  * @param content
  * @param url
  * @return
 

     
     
     
      */
 public byte[] format9(String content,String url){
//  第一条:

//  0B                  WAPPUSH头部的总长度,不包括本字节
//  05                  IE=16bit port addressing
//  04                  lenght of ports
//  0B84                dest. port
//  23F0                source port
//  00                  表示是Concatenated Short Messages
//  03                  udh头长度
//  B3                  reference number
//  02                  02是指共有两条短信
//  01                  01是指这是第一条
//  
//  接除UDH外的部分
//  ...
//  第二条:
//  0B                  WAPPUSH头部的总长度 ,不包括本字节
//  05040B8423F0        表示接下来是一个WAPPUSH     6字节
//  00                  表示是Concatenated Short Messages
//  03                  udh头长度
//  B3                  reference number
//  02                  02是指共有两条短信
//  02                  02是指这是第2条
//  
//  接余下部分
  
  byte[] WapPushUDH = new byte[]
                               {
    0x0B ,0x05  ,0x04  ,0x0B  ,(byte) 0x84  ,0x23  ,(byte) 0xF0  ,0x00  ,(byte) 0xB3  ,0x03  ,0x01  ,0x01
//    0x0B ,0x05  ,0x04  ,0x0B  ,(byte) 0x84  ,0x23  ,(byte) 0xF0  ,0x00  ,0x03  ,0x03  ,0x01  ,0x01
                               };
  //第二部分
  byte[] WapPushPUD = new byte[]
                               {
    (byte) 0xB3,0x06,0x01,(byte) 0xAE
//    0x29  ,0x06  ,0x06  ,0x03  ,(byte) 0xAE  ,(byte) 0x81  ,(byte) 0xEA  ,(byte) 0x8D  ,(byte) 0xCA
                               };
  
//  *pszPos++ = 0x02;;
//  *pszPos++ = 0x05; //-//WAPFORUM//DTD SI 1.0//EN
//  *pszPos++ = 0x6A; //UTF-8
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x45; //<si>
//  *pszPos++ = 0xC6; //<indication
//  *pszPos++ = 0x08; //<action=signal-high>
//  *pszPos++ = 0x0C; //href="http://"
//  *pszPos++ = 0x03; //字符串开始
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
//    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x0C, 0x03
    0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                                     };
  //第四部分:URL去掉http://后的UTF8编码
  //第五部分
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //>
//  *pszPos++ = 0x03; //字符串开始
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  //第六部分:消息文字的UTF8编码
//  //第七部分:
//  *pszPos++ = 0x00; //字符串结束
//  *pszPos++ = 0x01; //</indication>"
//  *pszPos++ = 0x01; //</si>
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   content=Utf8Util.getInstance().GBKToUtf8(content);
//   
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
//   System.out.println(this.byteToHexStr(bytContent));
   
   bytURL = urlUTF8.getBytes();
//   System.out.println(this.byteToHexStr(bytURL));
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128); //没有这行,NOKIA也可以接受,有也可以。
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
  
 }
 
 /**
  * @param content
  * @param url
  * @return 代替SmsPushData代码
  *  该代码NOKIA 和MOTO都可以收到
  *  有无长度控制,NOKIA都可以接收
  *  在WapPushPUD后 添加一个0x00,有无长度控制都可以接收NOKIA,可以考虑减少这个0X00
  */
 public byte[] format10(String content,String url){
  byte[] WapPushUDH = new byte[]
                               {
    0x06,0x05,0x04,0x0b,(byte) 0x84,0x23,(byte) 0xf0
                               };
  
  
  byte[] WapPushPUD = new byte[]
                               {
    
    (byte) 0x81,0x06,0x08,0x03,(byte) 0xae,(byte) 0x81,(byte) 0xea,(byte) 0xb4,(byte) 0x84,(byte) 0x8d ,0x00
                               };
  
  
  
  //第三部分
  byte[] WapPushIndicator = new byte[]
                                     {
    0x02,0x05,0x6a,0x00,0x45,(byte) 0xc6,0x08,0x0c,0x03
                                     };
  //第四部分:消息文字的UTF8编码
  //第五部分
  
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00,0x01,0x03
                                             };
  //第六部分:URL去掉http://后的UTF8编码
//  //第七部分:
  
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  

  int nurlLen;
  int ntipLen;
  int ncontent;

  String s = "";
  byte byte0 = 1;
  byte abyte0[] = new byte[4];
//  int i = 0;
  nurlLen = url.length() + 1;
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  
  bytURL=url.getBytes();
  bytContent  = this.chString2UTF8_byte(content);
  ntipLen = bytContent.length + 1;
  ncontent = nurlLen + ntipLen + 9 + 2 + 2;
  

  byte[] bytMsg=null;
  
  
  
  
//  getUIntVar(ncontent, abyte0, byte0);
  
  if(ncontent < 128)
  {
   abyte0[0] = (byte)(ncontent + 128);
   byte0 = 1;
   
  }
  if(ncontent < 16384)
  {
   byte0 = 2;
  } else
   if(ncontent < 0x200000)
   {
    byte0 = 3;
   } else
   {
    byte0 = 4;
   }
  ncontent <<= 32 - byte0 * 7;
  for(int j = 0; j < byte0 - 1; j++)
  {
   abyte0[j] |= 0x80;
  }
  
  for(int k = 0; k < byte0; k++)
  {
   abyte0[k] |= (byte)(ncontent>> 32 - (k + 1) * 7 & 0x7f);
  }
  
//  int ncontentLen = ncontent + WapPushUDH.length + WapPushPUD.length + byte0;
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen+ byte0;
//  否则MOTO A768不能正常接收
  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128); //在WapPushPUD后 添加一个0x00,有无长度控制都可以接收NOKIA
  
  
  
  bytMsg = new byte[totalLen]; 
  
  if(totalLen <= 140)
  {
   
   

   
   System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
   i+=nWapPushUDHLen;
   
   WapPushPUD[2] = (byte)((WapPushPUD.length - 3) + byte0);
   
   System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
   i+=nWapPushPUDLen;
   
   System.arraycopy(abyte0, 0, bytMsg, i, byte0);
   i += byte0; 
   
   System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
   i+=nPushIndicatorLen;
   
 
   System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
   i+=nBytURLLen;   
   
   System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
   i+=nPushDisplayTextHeaderLen;
   
   System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
   i+=nBytContentLen; 
   
   System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
   
  } else
  {
   //System.err.print((new StringBuilder()).append("/u4FE1/u606F/u957F/u5EA6/u8D85/u8FC770/u5B57/u7B26:").append(ncontentLen).toString());
  }
  return bytMsg;
  
 }
 
 /**
  * @param content
  * @param url
  * @return

                                  */
 public byte[] format11(String content, String url){
  
  
  byte[] WapPushUDH = new byte[]
                               {
    0x06, 0x05, 0x04, 0x0b, (byte) 0x84, 0x23,(byte) 0xf0
                               };
  
  byte[] WapPushPUD = new byte[]
                               {
    0x25, 0x06, 0x01, (byte) 0xae
                               };
  
  
  byte[] WapPushIndicator = new byte[]
                                     {
////    比老赵版本是01,部分代码是02
    0x02, 0x05, 0x6a, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03 //比老赵帮本多了一个,在这里;
                              };
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
//   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
//   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
   bytURL = urlUTF8.getBytes();
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
//  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
 }
 
 public byte[] format12(String content, String url){
  
  
  byte[] WapPushUDH = new byte[]
                               {
    0x06, 0x05, 0x04, 0x0b, (byte) 0x84, 0x23,(byte) 0xf0
                               };
//        result[i++] = 0x01; // push-id
//        result[i++] = 0x06; // pdu = push
//        result[i++] = 0x0a; // headlen=10
//        result[i++] = 0x03; // cert ??
//        result[i++] = (byte) 0xae; // application/vnd.wap.sic
//        result[i++] = (byte) 0x81; // charset
//        result[i++] = (byte) 0xea; // utf-8
//        result[i++] = (byte) 0xaf;
//        result[i++] = (byte) 0x82;
//        result[i++] = (byte) 0x8d; //
//        result[i++] = (byte) 0xae;
//        result[i++] = (byte) (Url.length + 159);
//        result[i++] = (byte) 0x87;
       
  byte[] WapPushPUD = new byte[]
                               {
    0x01, 0x06, 0x0a, 0x03,(byte) 0xae,(byte) 0x81,(byte) 0xea,(byte) 0xaf,(byte) 0x82,(byte) 0x8d,(byte) 0xae,0x00,(byte) 0x87
                               };
//        //SI part
//        result[i++] = 0x02;
//        result[i++] = 0x05; //-//WAPFORUM//DTD SI 1.0//EN
//        result[i++] = 0x6A; //UTF-8
//        result[i++] = 0x00; //字符串结束
//        result[i++] = 0x45; //<si>
//        result[i++] = (byte) 0xC6; //<indication
//        result[i++] = 0x08; //<action=signal-high>
//        result[i++] = 0x0C; //href="http://
//        result[i++] = 0x03; //字符串开始
//        for (int j = 0; j < Url.length;) {
//            result[i++] = Url[j++];
//        } //实际地址
//        result[i++] = 0x00; //字符串结束
//        // result[i++] = 0x0A; //created=
//        //result[i++] = (byte)0xC3; //'时间
//        // result[i++] = 0x07; //时间字节数
//        //result[i++] = ; //YYYYMMDDHHMMSSstartTime
//        // result[i++] = 0x10; //si_expires=
//        // result[i++] = (byte)0xC3; //时间
//        // result[i++] = 0x07; //时间字节数
//        //result[i++] = ; //YYYYMMDDHHMMSS
//        result[i++] = 0x01; //>
//        result[i++] = 0x03; //字符串开始endTime
//        for (int j = 0; j < Title.length;) {
//            result[i++] = Title[j++];
//        }
//        result[i++] = 0x00; //字符串结束
//        result[i++] = 0x01; //</indication>"
//        result[i++] = 0x01; //'</si>
  
  byte[] WapPushIndicator = new byte[]
                                     {
////    比老赵版本是01,部分代码是02
    0x02, 0x05, 0x6a, 0x00, 0x45, (byte) 0xC6,0x08,0x0C, 0x03
                              };
//  
  byte[] WapPushDisplayTextHeader = new byte[]
                                             {
    0x00, 0x01, 0x03
                                             };
  
  
  byte[] EndOfWapPush = new byte[]
                                 {
    0x00, 0x01, 0x01
                                 };
  
  byte[] bytContent = null;
  byte[] bytURL = null;
  try {
//   bytContent = content.getBytes();
//   bytURL = url.getBytes();
//   content = new String(bytContent, "UTF-8");
//   bytContent = content.getBytes("UTF-8");
//   String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
   
   String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
//   
//   bytContent = contentUTF8.getBytes();
   bytContent=this.chString2UTF8_byte(content);
   bytURL = urlUTF8.getBytes();
   WapPushPUD[WapPushPUD.length-2]=(byte) (bytURL.length +159);
   
  }
  catch(Exception ex) {
   ex.printStackTrace();  
  }
  
  int i=0;
  
  int nWapPushUDHLen=WapPushUDH.length;
  int nWapPushPUDLen=WapPushPUD.length;
  int nPushIndicatorLen=WapPushIndicator.length;
  int nBytURLLen=bytURL.length;
  int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
  int nBytContentLen=bytContent.length;
  int nEndOfWapPushLen=EndOfWapPush.length;
  
  int bodyLen=nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
  int totalLen=nWapPushUDHLen+nWapPushPUDLen+bodyLen;
//  否则MOTO A768不能正常接收
//  WapPushPUD[nWapPushPUDLen-1]=(byte) (bodyLen/2+128);
  
  
  
  byte[] bytMsg = new byte[totalLen]; 
  
  System.arraycopy(WapPushUDH, 0, bytMsg, i, nWapPushUDHLen);
  i+=nWapPushUDHLen;
  
  
  
  System.arraycopy(WapPushPUD, 0, bytMsg, i, nWapPushPUDLen);
  i+=nWapPushPUDLen;
  
  
  System.arraycopy(WapPushIndicator, 0, bytMsg, i, nPushIndicatorLen);
  i+=nPushIndicatorLen;
  
  
  System.arraycopy(bytURL, 0, bytMsg, i, nBytURLLen);
  i+=nBytURLLen;
  
  System.arraycopy(WapPushDisplayTextHeader, 0, bytMsg, i, nPushDisplayTextHeaderLen);
  i+=nPushDisplayTextHeaderLen;
  
  System.arraycopy(bytContent, 0, bytMsg, i, nBytContentLen);
  i+=nBytContentLen;
  
  System.arraycopy(EndOfWapPush, 0, bytMsg, i, nEndOfWapPushLen);
  
  return bytMsg;
 }
 
 private String getTimeString(Calendar calendar)
 {
  return String.valueOf(calendar.get(calendar.YEAR))
  + int2str(calendar.get(calendar.MONTH)+1)
  + int2str(calendar.get(calendar.DATE))
  + int2str(calendar.get(calendar.HOUR_OF_DAY))
  + int2str(calendar.get(calendar.MINUTE))
  + "00" ; //可以节省一个字节
  //+ int2str(calendar.get(calendar.SECOND)) ;
 }
 
 private byte[] getTimeBytes(String timeStr)
 {
  Vector by = new Vector();
  int temp = 0;
  boolean skip = true;
  for (int i=timeStr.length()/2-1; i>=0; i--){
   temp = Integer.parseInt(timeStr.substring(i*2, i*2+2));
//   System.out.println(temp);
   if (temp > 0 || temp==0 && !skip){
    if ( skip ){
     skip = false;
    }
    by.add(new Byte( (byte)(Integer.decode("0x" + String.valueOf(temp)).intValue()) ));
   }
  }
  byte[] bytes = new byte[by.size() + 1];
//  bytes[0] = (byte)0xC3;
  bytes[0] = (byte)(by.size());
  for (int i=0; i<by.size(); i++){
   bytes[i+1] = ((Byte)by.elementAt(by.size()-1-i)).byteValue();
  }
  return bytes;
 }
 
 private String int2str(int i){
  String s = String.valueOf(i);
  if (s.length()<2){
   s = "0" + s;
  }
  return s;
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  
  WapPushEncoder wapPushEncoder =new WapPushEncoder();
  
  System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format("f00的", "wap.ddd.net")));
  }
 
}
 

你可能感兴趣的:(exception,String,null,url,byte,Nokia)