一、短信平台 --莫名短信
该平台可以加后缀和自己定义发送内容,用户接收到的短信示例如下:
123456【圈圈科技】
二、webservice核心代码
这个是springMVC框架下的实现层代码,注释都在里面
@Service public class SendPhoneMessageServiceImpl implements SendPhoneMessageService{ @Autowired private MessageMapper messageMapper; public ResultObject sendSMS(SMSParams message) { ResultObject resultObject = new ResultObject(); resultObject.setResultCode(ResultCode.FAILED); //手机号码有效性判断 if(!isMobile(message.getPhoneNumber())){ resultObject.setResultMsg(ResultMsg.MSG_CHECKPHONE_FAILED); return resultObject; } //获取发送短信所需参数 // 账号 String smsAccount = ""; // 密码 String smsPwd = ""; // 接口地址 String smsURL = ""; // 发送内容 String smsContent = ""; SMSInfo messageInfo = messageMapper.getSMSInfo(); if(messageInfo != null) { //赋值 smsAccount = messageInfo.getSmsAccount(); smsPwd = messageInfo.getSmsPwd(); smsURL = messageInfo.getSmsUrl(); smsContent = messageInfo.getSmsContent(); //发送内容、地址不为空的情况 if(!smsAccount.isEmpty() && !smsURL.isEmpty() && smsURL.indexOf("{ACCOUNT}") != -1) { try{ smsContent = URLEncoder.encode(smsContent,"UTF-8"); }catch (UnsupportedEncodingException e) { e.printStackTrace(); } //用取到的数据代替设置的 smsURL = smsURL.replace("{ACCOUNT}", smsAccount) .replace("{PWD}", smsPwd) .replace("{PHONE}", message.getPhoneNumber()) .replace("{MESSAGE}",smsContent); } // 发送短息 System.out.println(smsURL); String retValue = requestURL(smsURL,"UTF-8"); if(retValue.equals("100")){ //发送成功 resultObject.setResultCode(ResultCode.SUCCESS); resultObject.setResultMsg(ResultMsg.MSG_SEND_SUCCESS); }else{ resultObject.setResultMsg(ResultMsg.MSG_GETSMSINFO_FAILED); } }else { resultObject.setResultMsg(ResultMsg.MSG_GETSMSINFO_FAILED); } return resultObject; } /** * 手机号验证 * * @param str * @return 验证通过返回true */ public static boolean isMobile(String str) { Pattern p = null; Matcher m = null; boolean b = false; // 验证手机号 p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); m = p.matcher(str); b = m.matches(); return b; } /** * 网络请求 * * @param String str 请求的网页地址 * @param String charSet 编码格式 * @return String 网页返回值 */ private String requestURL(String URLStr, String charSet){ // 网络的url地址 URL url = null; // 输入流 BufferedReader in = null; StringBuffer sb = new StringBuffer(); try{ url = new URL(URLStr); in = new BufferedReader( new InputStreamReader(url.openStream(),charSet) ); String str = null; while((str = in.readLine()) != null) { sb.append(str); } } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.getMessage()); } finally{ try{ if(in!=null) { in.close(); } }catch(Exception ex) { } } String result = sb.toString(); System.out.println(result); return result; }
时间: 2024-10-28 13:03:18