China Telecom SMS(SGIP)

来源:互联网 发布:php页面跳转url不变 编辑:程序博客网 时间:2024/06/08 17:54
import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.util.Properties;import sgip.tool.Bind;import sgip.tool.BindResp;import sgip.tool.SGIP_Command;import sgip.tool.Submit;import sgip.tool.SubmitResp;import sgip.tool.Unbind;import com.xx.entity.Message;import com.xx.util.MessageUtil;public class ChinaTelecomConnector implements Connector{public static final String UCS2 = "ISO-10646-UCS-2";private static final String ip = "SERVERIP";private static final String port = "PORT";private static final String username = "USERNAME";private static final String password = "PASSWORD";private static final String companypid = "COMPANYPID";private static final String spid = "SPID";private Socket socket = null;private String serverIp;private int serverPort;private String loginUsername;private String loginPassword;private Long loginCompanyId;private String loginSPID;private String chargerNumber;//public ChinaTelecomConnector(){}//由spring注入一些参数public ChinaTelecomConnector(Properties properties){intSocketParamters(properties);}private  void intSocketParamters(Properties properties){if(properties.containsKey(ip)){serverIp = (String) properties.get(ip);}if(properties.containsKey(port)){serverPort = Integer.parseInt((String) properties.get(port));}if(properties.containsKey(username)){loginUsername = (String) properties.get(username);}if(properties.containsKey(password)){loginPassword = properties.getProperty(password);}if(properties.containsKey(companypid)){loginCompanyId = Long.parseLong((String) properties.get(companypid));}if(properties.containsKey(spid)){loginSPID = (String) properties.get(spid);chargerNumber = loginSPID;}//socket = new Socket(serverIp, serverPort)}/** * 每一个Submit提交最后可以提交140字节,也是70个汉字,如果发送的是普通短信,TP_udhi为0,如果为长短信tp_udhi为1 * 发送长短信时,要将短信息UCS2编码,然后拆分成 67个字,也就是134个字段为一条短信用submit提交,因为还有6个字节要加th_udhiHead做为长短信的标识 * 这里结果,设为只要成功发送长信息中的一条短信成功,就返true */@Overridepublic  boolean sendSms(Message message,int count,String number) {// TODO Auto-generated method stubboolean bool = false;try {System.out.println(chargerNumber + " - " + loginSPID);//String sendContent = changeCharset(message.getContent(),GBK);String sendContent = message.getContent();socket = new Socket(serverIp, serverPort);OutputStream out = socket.getOutputStream();InputStream input = socket.getInputStream();SGIP_Command sgipCommand = new SGIP_Command();SGIP_Command tmp = null;BindResp bindResp = null;Submit submit = null;SubmitResp submitResp = null;Unbind unbind = null;//Step 1 -- BindBind bind = new Bind(Long.parseLong(loginSPID),1, loginUsername, loginPassword);bind.write(out);tmp = sgipCommand.read(input);if(tmp.getCommandID() == SGIP_Command.ID_SGIP_BIND_RESP){bindResp = (BindResp) tmp;bindResp.readbody();if(bindResp.GetResult() == 0){}}//Step 2 Submit//System.out.println("Content Length " + sendContent.length() );String[] messages = MessageUtil.splitMessage(sendContent);submit = new Submit(Long.parseLong(loginSPID),// node idloginSPID,//########################################SPNumberchargerNumber,//###############################################ChargeNumberscount,//############################################UserCountnumber,//###########################################UserNumbersloginCompanyId+"",//#######################################CorporateId corpId"",//##########################################ServiceType0,//################################################FeeType"0",//##############################################FeeValue"0",//##############################################GivenValue0,//################################################AgentFlag0,//################################################MorelatetoMTFlag0,//################################################Priority"",//###############################################ExpireTime"",//###############################################ScheduleTime1,//################################################ReportFlag0,//################################################TP_pidmessages.length > 1 ? 1 : 0,//######################TP_udhi8,//################################################MessageCoding0,//################################################MessageTypesendContent.length(),//####################MessageLength 最后两个字段的内容可以说是多余的,真正发送信息的是submit.setContent(8,ucs2String)发法sendContent//##############################MessageContent);//submit.setBinContent(8, sendContent.getBytes());byte[] allContentByte = sendContent.getBytes("ISO-10646-UCS-2");//System.out.println("byte Length " + allContentByte.length);int maxMsgLen = 140;for(int i=0;i<messages.length;i++){byte[] shortMessageUCSE = messages[i].getBytes("ISO-10646-UCS-2");byte[] tp_udhiHead = getTPUDHIHead(messages.length, i+1);// Get Tp_udhiHeadbyte[] contentBytes = null;if(messages.length > 1){//contentBytes = addByte(shortMessageUCSE, tp_udhiHead);//拆分短信字节和tp_udhiHead拼成submit需要提交字节,然后把转成字条串,再调用submit.setContent方法contentBytes = getSpliteLongMessageBytes(tp_udhiHead,allContentByte,i*(maxMsgLen - 6),(i+1)*(maxMsgLen-6));}else{contentBytes = shortMessageUCSE;}String ucs2String = new String(contentBytes,"ISO8859-1");submit.setContent(8,ucs2String);submit.write(out);out.flush();tmp = sgipCommand.read(input);if(tmp.getCommandID() == SGIP_Command.ID_SGIP_SUBMIT_RESP){submitResp = (SubmitResp) tmp;submitResp.readbody();if(submitResp.getResult() == 0){bool = true;//Successful Send}/*else{System.out.println(submitResp.getResult());bool = false;}*/}}//Step 3 Unbindunbind = new Unbind(Long.parseLong(loginSPID));unbind.write(out);} catch (Exception e) {e.printStackTrace();}return bool;}@Overridepublic String getResponse() {return null;}public static String changeCharset(String str, String newCharset) {String returnStr = null;try {if (str != null) {byte[] bs = str.getBytes();returnStr =  new String(bs, newCharset);}} catch (Exception e) {e.printStackTrace();}return returnStr;}public void setLoginSPID(String loginSPID) {this.loginSPID = loginSPID;}public void setChargerNumber(String chargerNumber) {this.chargerNumber = chargerNumber;}public static byte[] getSpliteLongMessageBytes(byte[] tpUdhiHead,byte[] messageUCS2 ,int start,int end){if(end > messageUCS2.length){end = messageUCS2.length; }byte[] msgb = new byte[end - start + 6];System.arraycopy(tpUdhiHead, 0, msgb, 0, 6);System.arraycopy(messageUCS2, start, msgb, 6, end - start);return msgb;}public static byte[] getTPUDHIHead(int messageLength,int number){byte[] tp_udhiHead = new byte[6];tp_udhiHead[0] = 0x05;tp_udhiHead[1] = 0x00;tp_udhiHead[2] = 0x03;tp_udhiHead[3] = 0x0A;tp_udhiHead[4] = (byte)messageLength;//长短信总长度tp_udhiHead[5] = (byte)number;//第几条短信return tp_udhiHead;}public static byte[] addByte(byte[] tp_udhiHead,byte[] content){int allLength = tp_udhiHead.length + content.length;byte[] udhi = new byte[allLength];for(int i = 0;i< tp_udhiHead.length;i++){udhi[i] = tp_udhiHead[i];}for(int j=0;j<content.length;j++){udhi[tp_udhiHead.length+j] = content[j];}return udhi;}}

原创粉丝点击