⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wappushencoder.java

📁 WAPPUSH的JAVA代码,已经通过了CMPP的测试,多种手机可以支持.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		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;
	}
	
	
	public byte[] format13(String content, String url){
		
		
		byte[] WapPushUDH = new byte[] 
		                             {
				0x0B,0x05,0x04,0x0B,(byte) 0x84,0x23,(byte) 0xF0,0x00,0x03,0x03,0x01,0x01,0x01,0x01
//				0x06, 0x05, 0x04, 0x0b, (byte) 0x84, 0x23,(byte) 0xf0
		                             };


//		 00 - UDH IE Tag
//		 03 - UDH SAR IE Length
//		 01 - Refrence
//		 02 - Total Packet 共有几条短信(此处2条)
//		 01 - Current Packet 这是第一条短信
		
		byte[] WapPushPUD = new byte[] 
		                             { 
				0x5F,0x06,0x0A,0x03,(byte) 0xAE,(byte) 0x81,(byte) 0xEA,(byte) 0xAF,(byte) 0x82,(byte) 0x8D,(byte) 0x90,(byte) 0xB4,(byte) 0x84
		                             };

		
		byte[] WapPushIndicator = new byte[] 
		                                   {
				0x02,0x05,0x6A,0x00,0x45,(byte) 0xC6,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.dg1860.ne")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format1("f01的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format2("f02的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format3("f03的", "wap.dg1860.com")));
//		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format4("f4的", "wap.dg1860.com")).length());
		
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format4("f04的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format5("f05的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format6("f06的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format7("f07的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format8("f08的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format9("f09的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format10("f10的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format11("f11的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format12("f12的", "wap.dg1860.com")));
		System.out.println(wapPushEncoder.byteToHexStr(wapPushEncoder.format13("f13的", "wap.dg1860.com")));
		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -