📄 smsproxy.java
字号:
package com.gctech.sms.client;
import java.net.InetAddress;
import java.net.Socket;
import java.io.OutputStream;
import com.gctech.util.Tools;
import java.io.*;
import org.apache.log4j.Logger;
import com.gctech.sms.msg.MTInfo;
import com.gctech.sms.msg.MTAck;
import com.gctech.sms.app.Constants;
/**
* <p>Title: 短信客户端代理。</p>
* <p>Description: 短信客户端代理。<br>
* 使用方法<br>
* SmsProxy.getInstance().send("01086073692", "你的校验码是223444", "NINE_CARD_REG");<br>
* 缺省的服务器地址和IP是211.99.23.231,9001,如果要改变服务器地址或IP,<br>
* 可以在进程启动时,执行<br>
* SmsProxy.getInstance().setHost(youSmsHost);<br>
* SmsProxy.getInstance().setPort(youSmsPort);<br>
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: gctech</p>
* @author 王红宝
* @see com.gctech.sms.platform.AppReciever
* @version $Id: SmsProxy.java,v 1.3 2004/06/22 08:13:34 wengjl Exp $
*/
public class SmsProxy {
public Object response(int msgType, Object req){
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(msgType);
out.writeObject(req);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
Object resp = in.readObject();
sock.close();
return resp;
}catch (Exception ex) {
logger.error(ex, ex);
return null;
}
}
public static void main(String[] args) {
//SmsProxy.getInstance().send("01080601027", "测试短信!", "ZX_SUB_CON");
MTInfo info = new MTInfo();
info.content = "快点拿来小灵通,王红宝!";
info.bDestNo = 1;
SmsProxy proxy = SmsProxy.getInstance();
proxy.setHost("localhost");
info.sDestPhone = "03715040034";
info.productID = "FREE_AD";
//if ( args.length > 2 )
info.gateway = 8;
//info.ValidTime = "20040312230000";
proxy.sendMt(info);
}
private SmsProxy() {
}
private static SmsProxy singleton;
public static SmsProxy getInstance() {
if (singleton == null) {
singleton = new SmsProxy();
}
return singleton;
}
/**
* 短信服务器端口.
* */
String host = "211.99.23.212";
/**
* 短信服务器端口.
* */
int port = 8002;
static final String PLATFORM = "platform";
/**
* 发送下行。
* */
public MTAck sendMt(MTInfo info) {
logger.debug(info);
MTAck ack = null;
Socket sock = null;
try {
InetAddress addr = InetAddress.getByName(host);
System.out.println("info:"+info+":host"+host+"addr:"+addr);
sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(Constants.MT_MSG);
out.writeObject(info);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
ack = (MTAck) in.readObject();
sock.close();
}
catch (Exception ex1) {
logger.error(ex1, ex1);
ack = new MTAck();
ack.setStatus( -102);
}
finally {
if (sock != null) {
try {
sock.close();
}
catch (IOException ex) {
logger.error(ex, ex);
}
}
}
return ack;
}
/*
public int batchSend(BatchSendRequest req){
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(Constants.ABCHINA_BATCH_MSG);
out.writeObject(req);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
BatchSendResponse ack = (BatchSendResponse) in.readObject();
sock.close();
return -120;
//return ack.getHead().getRtCode();
}catch (Exception ex) {
logger.error(ex, ex);
return -100;
}
}
*/
/**
* 发送短信接口
* @param destMobile 接受手机号
* @param content 内容(不超过140字符)
* @param businessId 业务代码
* @return 0 表示成功,-100连接服务器错误。
* */
public int send(String destMobile, String content, String businessId) {
/* try {
MTInfo info = new MTInfo();
info.content = content;
info.productID = businessId;
info.bDestNo = 1;
info.sDestPhone = destMobile;
info.spId = PLATFORM;
return this.sendMt(info).getStatus();
}
catch (Exception ex) {
logger.error(ex, ex);
return -100;
}*/
return send(destMobile,content,businessId,"");
}
/**
* 发送短信接口
* @param destMobile 接受手机号
* @param content 内容(不超过140字符)
* @param businessId 业务代码
* @param feeMobile 计费手机号码
* @return 0 表示成功,-100连接服务器错误。
* */
public int send(String destMobile, String content, String businessId,String feeMobile) {
try {
MTInfo info = new MTInfo();
info.content = content;
info.productID = businessId;
info.bDestNo = 1;
info.sDestPhone = destMobile;
info.spId = PLATFORM;
info.FeeterminalId = feeMobile;
return this.sendMt(info).getStatus();
}
catch (Exception ex) {
logger.error(ex, ex);
return -100;
}
}
/**
* 取消订阅。
*
public UnsubscribeResp unsubscribe(UnsubscribeReq req) {
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(Constants.UNSUBSCRIBE_MSG);
out.writeObject(req);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
UnsubscribeResp ack = (UnsubscribeResp) in.readObject();
sock.close();
return ack;
}
catch (Exception ex) {
logger.error(ex, ex);
UnsubscribeResp resp = new UnsubscribeResp();
MsgHead head = new MsgHead();
head.setCommandStatus( -100);
resp.setMsgHead(head);
return resp;
}
}
/**
* 订阅。
*
public SubscribeResp subscribe(SubscribeReq req) {
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(Constants.SUBSCRIBE_MSG);
out.writeObject(req);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
SubscribeResp ack = (SubscribeResp) in.readObject();
logger.debug(ack);
sock.close();
return ack;
}
catch (Exception ex) {
logger.error(ex, ex);
SubscribeResp resp = new SubscribeResp();
MsgHead head = new MsgHead();
head.setCommandStatus( -200);
resp.setMsgHead(head);
return resp;
}
}
/**
* 农行订阅接口。
* */
/*public int abchinaSub(SubscribeRequest request) {
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock = new Socket(addr, port);
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
out.writeInt(Constants.ABCHINA_SUBSCRIBE_MSG);
out.writeObject(request);
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
SubscribeResponse resp = (SubscribeResponse) in.readObject();
sock.close();
return resp.getRtCode();
}
catch (Exception ex) {
logger.error(ex, ex);
return -100;
}
}*/
//SubscribeRequest
// public
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public SmsProxy getSingleton() {
return singleton;
}
public void setSingleton(SmsProxy singleton) {
this.singleton = singleton;
}
public void setHost(String host) {
this.host = host;
}
public String getHost() {
return host;
}
static final Logger logger = Logger.getLogger(SmsProxy.class);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -