import java.net.URLEncoder;
import java.util.Random;
import com.bcloud.msg.http.HttpSender;//还需要一个http.jar包
public class SmsUtil{
private static final String URL_SendSM = "http://ip/msg/HttpSendSM"; //应用地址+单独发送方法
private static final String URL_BatchSendS = "http://ip/msg/HttpBatchSendS"; //应用地址+单独群发方法
private static final String ACCOUNT = "******";//账号
private static final String PSWD = "*******";//密码
private static final String SIGNATURE = "【我的技术总结】";
private static final String CONTENT_END = "退订回N";
private boolean needstatus = true;//是否需要状态报告,需要true,不需要false
private String Phone,Content;
private String Product = "";//产品ID
private String Extno = "012";//扩展码
private int Type = 1;//1,代表给一个手机号发短信,2,代表给多个人发短信
public SmsUtil(String phone,int type){
super();
this.Phone = phone;
this.Type = type;
}
public void setProduct(String product){
this.Product = product;
}
public void setExtno(String extno){
this.Extno = extno;
}
public String getProduct(){
return this.Product;
}
public String getExtno(){
return this.Extno;
}
public void setContent(String content){
this.Content = content;
}
public String getContent(){
return SIGNATURE+this.Content+CONTENT_END;
}
public void setNeedstatus(boolean flag){
this.needstatus = flag;
}
public Boolean getNeedstatus(){
return this.needstatus;
}
public String Send(){
String returnFlag = "";
if(Type == 1){
returnFlag +=Sendsms();
}else {
returnFlag +=batchSendsms();
}
return returnFlag;
}
//只给一个手机号码发送短信
public String Sendsms(){
String returnString = "";
try {
returnString = HttpSender.send(URL_SendSM, ACCOUNT, PSWD, Phone, SIGNATURE+Content+CONTENT_END, needstatus, Product, Extno);
// System.out.println(returnString);
return returnString;
//TODO 处理返回值,参见HTTP协议文档
} catch (Exception e) {
//TODO 处理异常
return returnString;
}
}
//给多个号码发送短信,用“,”隔开
public String batchSendsms(){
String returnString = "";
try {
returnString = HttpSender.batchSend(URL_BatchSendS, ACCOUNT, PSWD, Phone, SIGNATURE+Content+CONTENT_END, needstatus, Product, Extno);
// System.out.println(returnString);
return returnString;
//TODO 处理返回值,参见HTTP协议文档
} catch (Exception e) {
//TODO 处理异常
return returnString;
}
}
//得到随机数
public String getRandNum(int charCount) {
String charValue = "";
for (int i = 0; i < charCount; i++) {
char c = (char) (randomInt(0, 10) + '0');
charValue += String.valueOf(c);
}
return charValue;
}
public int randomInt(int from, int to) {
Random r = new Random();
return from + r.nextInt(to - from);
}
}