cmppwappush.java

来自「WAPPUSH的JAVA代码,已经通过了CMPP的测试,多种手机可以支持.」· Java 代码 · 共 239 行

JAVA
239
字号
/*
 * Created on 2005-1-3
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.wap;

import java.sql.Date;


public class CmppWapPush
{
	public CmppWapPush() 
	{
		
	}
	
	public int sendSMS(String[] mobile_nos, byte[] msg) 
	{
/*
		CMPPSubmitMessage submitMsg = 
			new gCMPPSubmitMessage(
				1,					//int pk_Total,
				1,					//int pk_Number,
				1,					//int registered_Delivery,
				0,					//int msg_Level,
				"free",			//String service_Id,
				1,					//int fee_UserType,
				"",					//String fee_Terminal_Id,
				0,					//int tp_Pid,
				1,					//int tp_Udhi,
				4,					//int msg_Fmt,
				"920101",			//String msg_Src,
				"01",				//String fee_Type,
				"0",				//String fee_Code,
				new Date(System.currentTimeMillis()),				//Date valid_Time,
				new Date(System.currentTimeMillis()),				//Date at_Time,
				"02009",			//String src_Terminal_Id,
				mobile_nos,			//String[] dest_Terminal_Id,
				msg,				//byte[] msg_Content,
				""					//String reserve
				) ;
*/
//		try 
//		{
///*
//			CMPPSubmitRepMessage submitRepMsg = 
//				(CMPPSubmitRepMessage)super.send(submitMsg);
//			System.out.println("send sms status is :" + submitRepMsg.getResult());
//*/
//				} 
//		catch (IOException e) 
//		{
//			System.out.println(e.getMessage());
//		}
		return 0;
	}

	private static String getSMSPush(String url, String subject, String startTime,
									 String endTime)
	{
		String pushString = "";
		String body = "";
		body += "02";
		body += "05"; //-//WAPFORUM//DTD SI 1.0//EN
		body += "6A"; //UTF-8
		body += "00"; //字符串结�?
		body += "45"; //<si>
		body += "C6"; //<indication
		body += "08"; //<action=signal-high>
		body += "0C"; //href="http://
		body += "03"; //字符串开�?
		body += byteArrayToHexString(url.getBytes()); //实际地址
		body += "00"; //字符串结�?
		body += "0A"; //created=
		body += "C3"; //'时间
		body += "07"; //时间字节�?
		body += startTime; //YYYYMMDDHHMMSS
		body += "10"; //si_expires=
		body += "C3"; //时间
		body += "07"; //时间字节�?
		body += endTime; //YYYYMMDDHHMMSS
		body += "01"; //>
		body += "03"; //字符串开�?
		try
		{
			body += byteArrayToHexString(subject.getBytes("utf-8")); //显示给用户的内容,用utf-8编码。utf-8编码,英文字符直接用ascii码;中文如果unicode是(二进制)
		}
		catch (Exception ex)
		{
		}
		body += "00"; //字符串结�?
		body += "01"; //</indication>"
		body += "01"; //'</si>
		int length = body.length();
		String pud = "";
		pud += "81"; //transaction id (connectionless WSP)
		pud += "06"; //'pdu type (06=push)
		pud += "06"; //Headers len
		pud += "03";
		pud += "AE";
		pud += "81";
		pud += "EA"; //content type: application/vnd.wap.sic; charset=utf-8
		pud += "8D"; //content-length
		pud += Integer.toHexString(length).toUpperCase();
		String udh = "";
		udh += "06"; //User Data Header Length (6 bytes)
		udh += "05"; //UDH Item Element id (Port Numbers)
		udh += "04"; //UDH IE length (4 bytes)
		udh += "0B";
		udh += "84"; //destination port number
		udh += "23";
		udh += "F0"; //origin port number
		pushString = udh + pud + body;
		System.out.println(pushString);
		return pushString;
	}
	
	public static String byteArrayToHexString(byte b[])
	{
		String result = "";
		for (int i = 0; i < b.length; i++)
			result = result + byteToHexString(b[i]);
		return result;
	}

	public static String byteToString(byte b[])
	{	
		String result = "";
		for (int i = 0; i < b.length; i++)
		{
			result = result + b[i];
		}
		return result;
	}

	public static String byteToHexString(byte b)
	{
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return HexCode[d1] + HexCode[d2];
	}

	private static String HexCode[] =
	{
		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
		"A", "B", "C", "D", "E", "F"
	};

	private static String getUTFString(final String gbString)
	{
		if (gbString == null)
			return "";
		char[] utfBytes = gbString.toCharArray();
		String unicodeBytes = "";
		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++)
		{
			String hexB = "";
			if (utfBytes[byteIndex] < '!')
			{
				hexB = Integer.toHexString(utfBytes[byteIndex]);
				if (hexB.length() <= 2)
				{
					hexB = "00" + hexB;
				}
				unicodeBytes = unicodeBytes + "&#x" + hexB + ";";
			}
			else
			{
				unicodeBytes += utfBytes[byteIndex];
			}
		}
		return unicodeBytes;
	}
	
	public static int stringToByte(String str)
	{
		int ret = 0;
		for(int i=0;i<HexCode.length;i++)
		{
			if(HexCode[i].equalsIgnoreCase(str))
				return i;
		}	
		return ret;
	}
	
	public static byte[] stringToBytes(String str)
	{
		if(str==null||str.equals(""))
			return null;
		
		byte[] bytes = new byte[str.length()/2];
		for(int i=0;i<bytes.length;i++)
		{
			String high = str.substring(i*2,i*2+1);
			String low = str.substring(i*2+1,i*2+2);
			bytes[i] = (byte) (stringToByte(high)*16 + stringToByte(low));
		}
		return bytes;
	}
	
	public static void main(String[] args)
	{
		//启动短信下行代理
		CmppWapPush cmppProxy = new CmppWapPush();
//		System.out.println("succeeded load cmpp SMS send proxy");
				
				
		try 
		{
			Thread.sleep(6000);
		} 
		catch (InterruptedException e1) 
		{
			e1.printStackTrace();
		}
				
		String[] mobile_nos = {"13512345678"};
		

		int i = cmppProxy.sendSMS(mobile_nos,
			CmppWapPush.stringToBytes(
				CmppWapPush.getSMSPush("wap.dg1860.com","1860",
					new Date(System.currentTimeMillis()).toString(),
					new Date(System.currentTimeMillis()).toString())));

		
		String content = "0b05040b8423f0000303010103060403ae81ea02056a0045c60c033231312e3133362e38362e3232362f54574652656c61792f6a6c676d63632f707573685f72696e672e6a737000070103e4b88be8bdbde885bee8aeafe5bda9e993832ce98081e5858de8b4b9e99383e99fb3000101";
//		int i = cmppProxy.sendSMS(mobile_nos,
//			CmppWapPush.stringToBytes(content));
		
		System.out.println("return is :" + i);
	}
}

⌨️ 快捷键说明

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