我来给你讲解下吧 具体看代码
properties:
smsName=你自己充值后的用户名
smsPwd=密码
smsInterface=http://106.ihuyi.cn/webservice/sms.php?method=Submit //我用的是这个公司的发送短信接口,还是比较快的 ,大概延迟2-5秒 收到短信service:
public void sendSMS(String phone,String content) throws Exception{
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(PropertiesUtil.getString("smsInterface"));
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
NameValuePair[] data = {//提交短信
new NameValuePair("account", PropertiesUtil.getString("smsName")),
new NameValuePair("password", PropertiesUtil.getString("smsPwd")), //密码可以使用明文密码或使用32位MD5加密
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", phone),
new NameValuePair("content",content),
}; method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
if(code.equals("2")){
logger.info("sms send ok");
}else{
logger.info("sms maybe senderror code:"+code+" msg;"+msg);
throw new Exception("sms send code is error");
} } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @Transactional
public void cardPhoneBindingNoLogin(String phoneNum,Card card) throws Exception{
String smsCode = Encrypt.random(6);
String msg = "您的验证码是:"+smsCode+"。请不要把验证码泄露给其他人。如非本人操作,可不用理会!";
card.setLastSMSPhone(phoneNum);
card.setLastSMSCode(smsCode);
card.setLastSMSTime(new Date().getTime());
card.merge();
sendSMS(phoneNum, msg);
}
properties:
smsName=你自己充值后的用户名
smsPwd=密码
smsInterface=http://106.ihuyi.cn/webservice/sms.php?method=Submit //我用的是这个公司的发送短信接口,还是比较快的 ,大概延迟2-5秒 收到短信service:
public void sendSMS(String phone,String content) throws Exception{
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(PropertiesUtil.getString("smsInterface"));
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
NameValuePair[] data = {//提交短信
new NameValuePair("account", PropertiesUtil.getString("smsName")),
new NameValuePair("password", PropertiesUtil.getString("smsPwd")), //密码可以使用明文密码或使用32位MD5加密
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", phone),
new NameValuePair("content",content),
}; method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
if(code.equals("2")){
logger.info("sms send ok");
}else{
logger.info("sms maybe senderror code:"+code+" msg;"+msg);
throw new Exception("sms send code is error");
} } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @Transactional
public void cardPhoneBindingNoLogin(String phoneNum,Card card) throws Exception{
String smsCode = Encrypt.random(6);
String msg = "您的验证码是:"+smsCode+"。请不要把验证码泄露给其他人。如非本人操作,可不用理会!";
card.setLastSMSPhone(phoneNum);
card.setLastSMSCode(smsCode);
card.setLastSMSTime(new Date().getTime());
card.merge();
sendSMS(phoneNum, msg);
}
properties:
smsName=你自己充值后的用户名
smsPwd=密码
smsInterface=http://106.ihuyi.cn/webservice/sms.php?method=Submit //我用的是这个公司的发送短信接口,还是比较快的 ,大概延迟2-5秒 收到短信service:
public void sendSMS(String phone,String content) throws Exception{
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(PropertiesUtil.getString("smsInterface"));
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
NameValuePair[] data = {//提交短信
new NameValuePair("account", PropertiesUtil.getString("smsName")),
new NameValuePair("password", PropertiesUtil.getString("smsPwd")), //密码可以使用明文密码或使用32位MD5加密
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", phone),
new NameValuePair("content",content),
}; method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
if(code.equals("2")){
logger.info("sms send ok");
}else{
logger.info("sms maybe senderror code:"+code+" msg;"+msg);
throw new Exception("sms send code is error");
} } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @Transactional
public void cardPhoneBindingNoLogin(String phoneNum,Card card) throws Exception{
String smsCode = Encrypt.random(6);
String msg = "您的验证码是:"+smsCode+"。请不要把验证码泄露给其他人。如非本人操作,可不用理会!";
card.setLastSMSPhone(phoneNum);
card.setLastSMSCode(smsCode);
card.setLastSMSTime(new Date().getTime());
card.merge();
sendSMS(phoneNum, msg);
}
properties:
smsName=你自己充值后的用户名
smsPwd=密码
smsInterface=http://106.ihuyi.cn/webservice/sms.php?method=Submit //我用的是这个公司的发送短信接口,还是比较快的 ,大概延迟2-5秒 收到短信service:
public void sendSMS(String phone,String content) throws Exception{
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(PropertiesUtil.getString("smsInterface"));
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
NameValuePair[] data = {//提交短信
new NameValuePair("account", PropertiesUtil.getString("smsName")),
new NameValuePair("password", PropertiesUtil.getString("smsPwd")), //密码可以使用明文密码或使用32位MD5加密
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", phone),
new NameValuePair("content",content),
}; method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
if(code.equals("2")){
logger.info("sms send ok");
}else{
logger.info("sms maybe senderror code:"+code+" msg;"+msg);
throw new Exception("sms send code is error");
} } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
用短信猫的话,厂家也会提供API和DEMO。
最麻烦的情况,是自己写串口+AT指令代码。