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;
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 WapPush编码;
public String format2(String content, String url){
// str1 = "0605040B8423F0250601AE01056A0045C60C03" '赵版本
// retStr = str1 & strURLHexAscii & "000103" & URLTipsUTF8 & "000101" '赵版本
//
String head1=null;
head1="0605040B8423F0250601AE01056A0045C60C03"; //老赵版本;
// head1="0B05040B8423F0000303010129060603AE81EA8DCA02056A0045C60C03"; //网络版本;
String mid1=null,tail1=null;
mid1="000103";
tail1="000101";
String ret=null;
byte[] bytContent = null;
byte[] bytURL = null;
try {
String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
bytContent = contentUTF8.getBytes();
bytURL = urlUTF8.getBytes();
}
catch(Exception ex) {
ex.printStackTrace();
}
ret=head1+this.byteToHexStr(bytURL)+mid1+this.byteToHexStr(bytContent)+tail1;
return ret;
}
*/
/*
public String format3(String content,String url){
// //第一部分
// static final private byte[] WapPushHeader1 = new byte[]
// {
// 0x0B, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0, 0x00, 0x03, 0x03, 0x01, 0x01
// };
// //第二部分
// static final private byte[] WapPushHeader2 = new byte[]
// {
// 0x29, 0x06, 0x06, 0x03, (byte) 0xAE, (byte) 0x81, (byte) 0xEA, (byte) 0x8D, (byte) 0xCA
// };
//
// //第三部分
// static final private byte[] WapPushIndicator = new byte[]
// {
// 0x02, 0x05, 0x6A, 0x00, 0x45, (byte) 0xC6, 0x0C, 0x03
// };
// //第四部分:URL去掉http://后的UTF8编码
// //第五部分
// static final private byte[] WapPushDisplayTextHeader = new byte[]
// {
// 0x00, 0x01, 0x03,
// };
// //第六部分:消息文字的UTF8编码
// //第七部分:
// static final private 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";
String ret=null;
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();
// System.out.println(this.byteToHexStr(bytContent));
bytURL = urlUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytURL));
}
catch(Exception ex) {
ex.printStackTrace();
}
ret=wapPushHeader1+wapPushHeader2+wapPushIndicator+this.byteToHexStr(bytURL)+wapPushDisplayTextHeader+this.byteToHexStr(bytContent)+wapPushEnd;
return ret;
}
*/
public byte[] format5(String content,String url){
//第一部分
byte[] WapPushHeader1 = new byte[]
{
0x06, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0
// 0x0B, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0, 0x00, 0x03, 0x03, 0x01, 0x01
};
//第二部分
byte[] WapPushHeader2 = new byte[]
{
0x25,0x06,0x01,(byte) 0xae
// 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编码
//第五部分
// *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 nPushHeader1Len=WapPushHeader1.length;
int nPushHeader2Len=WapPushHeader2.length;
int nPushIndicatorLen=WapPushIndicator.length;
int nBytURLLen=bytURL.length;
int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
int nBytContentLen=bytContent.length;
int nEndOfWapPushLen=EndOfWapPush.length;
int totalLen=nPushHeader1Len+nPushHeader2Len+nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
byte[] bytMsg = new byte[totalLen];
System.arraycopy(WapPushHeader1, 0, bytMsg, i, nPushHeader1Len);
i+=nPushHeader1Len;
System.arraycopy(WapPushHeader2, 0, bytMsg, i, nPushHeader2Len);
i+=nPushHeader2Len;
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
*/
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);
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
*/
public byte[] format3(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
};
// *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
* 网络上算法:
* 1. 消息头,包含两部分,UDH和PUD,现在经过测试,有三种可以使用的包头数据.
包头a:
UDH: 06 05 04 0B 84 23 F0
PUD: 01 06 03 AE 8D C4
包头b:
UDH: 0B 05 04 0B 84 23 F0 00 03 03 01 01
PUD: 29 06 06 03 AE 81 EA 8D CA
包头c:
UDH: 06 05 04 0B 84 23 F0
PUD: 81 06 06 03 AE 81 EA 8D 00
2. 消息体
02
05 //WAPFORUM//DTD SI 1.0//EN
6A //UTF-8
00 //字符串结束
45 //<si>
C6 //<indication
08 //<action=signal-high>
0C //href="http://"
03 //字符串开始
.... //URL字符串,要UTF8编码
00 //字符串结束
01 //>
03 //字符串开始
.... //内容描述字符串,要UTF8编码
00 //字符串结束
01 //</indication>
01 //</si>
3.
生成消息包体后,要在PUD包头中设置包体的长度,PUD头中的最后一个字节表示包体的长度.
PUD[len(PUD)-1] = len(BODY)/2 + 128;
4. 生成短信内容消息数据
SMSDATA = UDH + PUD + BODY
SMSDATA在短消息中作为消息内容
5. 短信息发送
使用CMPP协议向中国移动网关发送短消息时,要设定MsgFormat为4,表示数据是二进制格式.
6. 测试结果
使用各种包头数据,所支持的手机型号不同.测试结果如下:
包头a: 索爱K750c, Nokia3230, Nokia6270
包头b: 索爱K750c, Nokia3230, Nokia6270, 多普达ppc696
包头c: 索爱K750c, 索爱K700, MOTO A768, Nokia3230, Nokia6270
附1: 第三个包头的详细描述
// UDH
06 //User Data Header Length (6 bytes)
05 //UDH Item Element id (Port Numbers)
04 //UDH IE length (4 bytes)
0B
84 //destination port number
23
F0 //origin port number
// PUD
81 //transaction id (connectionless WSP)
06 //pdu type (06=push)
06 //Headers len
03
AE
81
EA //content type: application/vnd.wap.sic; charset=utf-8
8D //content-length
INT //body length
包头c:
UDH: 06 05 04 0B 84 23 F0
PUD: 81 06 06 03 AE 81 EA 8D 00
*/
/*
public String format4(String content,String url){
String ret="";
// guanzhong 2006-2-22, 使用了第三个包头
// unsigned char szUDH[] = {0x06,0x05,0x04,0x0B,0x84,0x23,0xF0};
String UDH="0605040B8423F0";
// unsigned char szPUD[] = {0x81,0x06,0x06,0x03,0xAE,0x81,0xEA,0x8D,0x00};
String PDU="81060603AE81EA8D";
// // guanzhong
//
// unsigned char szMsg[280]={0};
//
// int nUDHLen = sizeof(szUDH);
int nUDHLen=UDH.length()/2;
// int nPUDLen = sizeof(szPUD);
int nPDULen=PDU.length()/2+1; //包括PDU长度;
// unsigned char *pszBody = szMsg + nUDHLen + nPUDLen;
//
// unsigned char *pszPos = pszBody;
//
// *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[] bytContent = null;
byte[] bytURL = null;
String contentUTF8="";
String urlUTF8="";
try {
// content=Utf8Util.getInstance().GBKToUtf8(content);
//
// bytContent = content.getBytes();
// bytURL = url.getBytes();
// content = new String(bytContent, "UTF-8");
// bytContent = content.getBytes("UTF-8");
contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
bytContent = contentUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytContent));
bytURL = urlUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytURL));
}
catch(Exception ex) {
ex.printStackTrace();
}
String PushBodyHeader="02056A0045C6080C03";
// String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
// String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
String wapPushDisplayTextHeader="000103";
// 第六部分:消息文字的UTF8编码
String wapPushEnd="000101";
// strcpy((char*)pszPos,szUTF8Url);
// pszPos += strlen(szUTF8Url);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //>
// *pszPos++ = 0x03; //字符串开始
//
// strcpy((char*)pszPos,szUTF8Info);
// pszPos += strlen(szUTF8Info);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //</indication>"
// *pszPos++ = 0x01; //</si>
//
String body=PushBodyHeader+this.byteToHexStr(bytURL)+wapPushDisplayTextHeader+this.byteToHexStr(bytContent)+wapPushEnd;
// //设置包体的长度
// //szPUD[nPUDLen-1] = pszPos-pszBody;
// // guanzhong 2006-3-3 修改,否则MOTO A768不能正常接收
// szPUD[nPUDLen-1] = (pszPos-pszBody)/2 + 128;
// int bodyLen=(body.length()/2-nUDHLen-nPDULen)/2+128;
int bodyLen=body.length()/2+128;
System.out.println("bodyLen:"+bodyLen);
//
// memcpy(szMsg,szUDH,nUDHLen);
// memcpy(szMsg + nUDHLen,szPUD,nPUDLen);
// nMsgLen = pszPos - szMsg;
//
// memcpy(pszData,szMsg,nMsgLen);
// return TRUE;
ret=UDH+PDU+this.intToHexStr(bodyLen)+body;
return ret;
}
*/
/**
* @param content
* @param url
* @return
* 包头a:
UDH: 06 05 04 0B 84 23 F0
PUD: 01 06 03 AE 8D C4
*/
/*
public String format41(String content,String url){
String ret="";
// guanzhong 2006-2-22, 使用了第三个包头
// unsigned char szUDH[] = {0x06,0x05,0x04,0x0B,0x84,0x23,0xF0};
String UDH="0605040B8423F0";
// unsigned char szPUD[] = {0x81,0x06,0x06,0x03,0xAE,0x81,0xEA,0x8D,0x00};
String PDU="010603AE8DC4";
// // guanzhong
//
// unsigned char szMsg[280]={0};
//
// int nUDHLen = sizeof(szUDH);
int nUDHLen=UDH.length()/2;
// int nPUDLen = sizeof(szPUD);
int nPDULen=PDU.length()/2+1; //包括PDU长度;
// unsigned char *pszBody = szMsg + nUDHLen + nPUDLen;
//
// unsigned char *pszPos = pszBody;
//
// *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[] bytContent = null;
byte[] bytURL = null;
String contentUTF8="";
String urlUTF8="";
try {
// content=Utf8Util.getInstance().GBKToUtf8(content);
//
// bytContent = content.getBytes();
// bytURL = url.getBytes();
// content = new String(bytContent, "UTF-8");
// bytContent = content.getBytes("UTF-8");
contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
bytContent = contentUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytContent));
bytURL = urlUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytURL));
}
catch(Exception ex) {
ex.printStackTrace();
}
String PushBodyHeader="02056A0045C6080C03";
// String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
// String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
String wapPushDisplayTextHeader="000103";
// 第六部分:消息文字的UTF8编码
String wapPushEnd="000101";
// strcpy((char*)pszPos,szUTF8Url);
// pszPos += strlen(szUTF8Url);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //>
// *pszPos++ = 0x03; //字符串开始
//
// strcpy((char*)pszPos,szUTF8Info);
// pszPos += strlen(szUTF8Info);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //</indication>"
// *pszPos++ = 0x01; //</si>
//
String body=PushBodyHeader+this.byteToHexStr(bytURL)+wapPushDisplayTextHeader+this.byteToHexStr(bytContent)+wapPushEnd;
// //设置包体的长度
// //szPUD[nPUDLen-1] = pszPos-pszBody;
// // guanzhong 2006-3-3 修改,否则MOTO A768不能正常接收
// szPUD[nPUDLen-1] = (pszPos-pszBody)/2 + 128;
// int bodyLen=(body.length()/2-nUDHLen-nPDULen)/2+128;
int bodyLen=body.length()/2+128;
System.out.println("bodyLen:"+bodyLen);
//
// memcpy(szMsg,szUDH,nUDHLen);
// memcpy(szMsg + nUDHLen,szPUD,nPUDLen);
// nMsgLen = pszPos - szMsg;
//
// memcpy(pszData,szMsg,nMsgLen);
// return TRUE;
// ret=UDH+PDU+this.intToHexStr(bodyLen)+body; 有长度值
ret=UDH+PDU+body;
return ret;
}
*/
/**
* @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
*/
/*
public String format42(String content,String url){
String ret="";
// guanzhong 2006-2-22, 使用了第三个包头
// unsigned char szUDH[] = {0x06,0x05,0x04,0x0B,0x84,0x23,0xF0};
String UDH="0B05040B8423F00003030101";
// unsigned char szPUD[] = {0x81,0x06,0x06,0x03,0xAE,0x81,0xEA,0x8D,0x00};
String PDU="29060603AE81EA8DCA";
// // guanzhong
//
// unsigned char szMsg[280]={0};
//
// int nUDHLen = sizeof(szUDH);
int nUDHLen=UDH.length()/2;
// int nPUDLen = sizeof(szPUD);
int nPDULen=PDU.length()/2+1; //包括PDU长度;
// unsigned char *pszBody = szMsg + nUDHLen + nPUDLen;
//
// unsigned char *pszPos = pszBody;
//
// *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[] bytContent = null;
byte[] bytURL = null;
String contentUTF8="";
String urlUTF8="";
try {
// content=Utf8Util.getInstance().GBKToUtf8(content);
//
// bytContent = content.getBytes();
// bytURL = url.getBytes();
// content = new String(bytContent, "UTF-8");
// bytContent = content.getBytes("UTF-8");
contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
bytContent = contentUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytContent));
bytURL = urlUTF8.getBytes();
// System.out.println(this.byteToHexStr(bytURL));
}
catch(Exception ex) {
ex.printStackTrace();
}
String PushBodyHeader="02056A0045C6080C03";
// String contentUTF8=Utf8Util.getInstance().GBKToUtf8(content);
// String urlUTF8=Utf8Util.getInstance().GBKToUtf8(url);
String wapPushDisplayTextHeader="000103";
// 第六部分:消息文字的UTF8编码
String wapPushEnd="000101";
// strcpy((char*)pszPos,szUTF8Url);
// pszPos += strlen(szUTF8Url);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //>
// *pszPos++ = 0x03; //字符串开始
//
// strcpy((char*)pszPos,szUTF8Info);
// pszPos += strlen(szUTF8Info);
//
// *pszPos++ = 0x00; //字符串结束
// *pszPos++ = 0x01; //</indication>"
// *pszPos++ = 0x01; //</si>
//
String body=PushBodyHeader+this.byteToHexStr(bytURL)+wapPushDisplayTextHeader+this.byteToHexStr(bytContent)+wapPushEnd;
// //设置包体的长度
// //szPUD[nPUDLen-1] = pszPos-pszBody;
// // guanzhong 2006-3-3 修改,否则MOTO A768不能正常接收
// szPUD[nPUDLen-1] = (pszPos-pszBody)/2 + 128;
// int bodyLen=(body.length()/2-nUDHLen-nPDULen)/2+128;
int bodyLen=body.length()/2+128;
System.out.println("bodyLen:"+bodyLen);
//
// memcpy(szMsg,szUDH,nUDHLen);
// memcpy(szMsg + nUDHLen,szPUD,nPUDLen);
// nMsgLen = pszPos - szMsg;
//
// memcpy(pszData,szMsg,nMsgLen);
// return TRUE;
// ret=UDH+PDU+this.intToHexStr(bodyLen)+body;
ret=UDH+PDU+body;
return ret;
}
*/
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[] 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 iContentLen = bytContent.length;
int iURLLen = bytURL.length;
byte[] bytMsg = new byte[26 + iContentLen + iURLLen];
// System.out.println("format4 total len:"+(25 + iContentLen + iURLLen));
// int ToWapPush(char *title, char *url, char *buf)
// {
//// 这个是我用来转换编码的一个函数,你可以不管
// GbToUtf8( title );
// //7字节的控制信息
// sprintf( tmpstr,"%c%c%c%c%c%c%c", 0x06, 0x05, 0x04, 0x0B, 0x84, 0x23, 0xF0);
// memcpy( buf, tmpstr, 7 );
// UDLen = 7;
//// 4字节的控制信息
// sprintf( tmpstr, "%c%c%c%c", 0x25, 0x06, 0x01, 0xAE );
// memcpy( buf+UDLen, tmpstr, 4);
// UDLen += 4;
//// 8字节的控制信息
// sprintf( tmpstr,"%c%c%c%c%c%c%c%c%c",
// 0x01, 0x05, 0x6A, 0x00, 0x45, 0xC6, 0x0C, 0x03);
// memcpy( buf+UDLen, tmpstr, 8);
// UDLen += 8;
//
// memcpy(buf+UDLen, url, strlen(url));
// UDLen += strlen(url);
// buf[UDLen++] = '/0';
//// 结尾的控制字段
// buf[UDLen++] = 01;
// buf[UDLen++] = 03;
//
// memcpy(buf+UDLen, title, strlen(title));
// UDLen += strlen(title);
//
// buf[UDLen++] = '/0';
// buf[UDLen++] = 01;
// buf[UDLen++] = 01;
// return UDLen;
// }
int i = 0;
// 7字节的控制信息
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;
// 4字节的控制信息
bytMsg[i++] = (byte)0x25;
bytMsg[i++] = (byte)0x06;
bytMsg[i++] = (byte)0x01;
bytMsg[i++] = (byte)0xae;
// 8字节的控制信息
bytMsg[i++] = (byte)0x02; ////比老赵版本是01,
bytMsg[i++] = (byte)0x05;
bytMsg[i++] = (byte)0x6a;
bytMsg[i++] = (byte)0x00;
bytMsg[i++] = (byte)0x45;
bytMsg[i++] = (byte)0xc6;
bytMsg[i++] = (byte)0x08; //比老赵帮本多了一个,在这里;
bytMsg[i++] = (byte)0x0c;
bytMsg[i++] = (byte)0x03;
System.arraycopy(bytURL, 0, bytMsg, i, iURLLen);
i += iURLLen;
bytMsg[i++] = (byte)0x00;
bytMsg[i++] = (byte)0x01;
bytMsg[i++] = (byte)0x03;
System.arraycopy(bytContent, 0, bytMsg, i, iContentLen);
i += iContentLen;
bytMsg[i++] = (byte)0x00;
bytMsg[i++] = (byte)0x01;
bytMsg[i++] = (byte)0x01;
return bytMsg;
}
public byte[] format7(String content,String url){
Calendar calendar = Calendar.getInstance();
String currentTime = getTimeString(calendar);
calendar.add(calendar.MONTH, 1); //一个月以后的时间
String nextTime = getTimeString(calendar);
byte[] startTime= getTimeBytes(currentTime);
byte[] endTime = getTimeBytes(nextTime);
// 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
// (byte) 0x81,0x06,0x06,0x03,(byte) 0xAE,(byte) 0x81,(byte) 0xEA,(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,0x01, 0x07,
};
// body = body + endTime;
// body = body + "01";
// body = body + "03";
//
byte[] WapPushDisplayTextHeader3 = new byte[]
{
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 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;
}
public byte[] format8(String content,String url){
//第一部分
byte[] WapPushHeader1 = new byte[]
{
// 0x06, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0
0x0B, 0x05, 0x04, 0x0B, (byte) 0x84, 0x23, (byte) 0xF0, 0x00, 0x03, 0x03, 0x01, 0x01
};
//第二部分
byte[] WapPushHeader2 = new byte[]
{
// 0x25,0x06,0x01,(byte) 0xae
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编码
//第五部分
// *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 nPushHeader1Len=WapPushHeader1.length;
int nPushHeader2Len=WapPushHeader2.length;
int nPushIndicatorLen=WapPushIndicator.length;
int nBytURLLen=bytURL.length;
int nPushDisplayTextHeaderLen=WapPushDisplayTextHeader.length;
int nBytContentLen=bytContent.length;
int nEndOfWapPushLen=EndOfWapPush.length;
int totalLen=nPushHeader1Len+nPushHeader2Len+nPushIndicatorLen+nBytURLLen+nPushDisplayTextHeaderLen+nBytContentLen+nEndOfWapPushLen;
byte[] bytMsg = new byte[totalLen];
System.arraycopy(WapPushHeader1, 0, bytMsg, i, nPushHeader1Len);
i+=nPushHeader1Len;
System.arraycopy(WapPushHeader2, 0, bytMsg, i, nPushHeader2Len);
i+=nPushHeader2Len;
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)) ;
}
//timeStr的格式: yyyymmddhhmiss
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));
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() + 2];
bytes[0] = (byte)0xC3;
bytes[1] = (byte)(by.size());
for (int i=0; i<by.size(); i++){
bytes[i+2] = ((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("f0的", "wap.ABC.com")));
}
}