`
2277259257
  • 浏览: 498459 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

发送短信验证---金猫实例

 
阅读更多

public void sendSMS(ModelMap map,HttpServletRequest request,HttpServletResponse response, @RequestParam String phone) throws NumberFormatException,Exception{ 
        组装参数:
        String url = "http://sdk2.entinfo.cn:8061/mdsmssend.ashx?"; //发送地址
        String sn = "SDK-HFY-010-00024";      //sn
        String pwd = "28728CB1AD539A991E7CB6BA91B349C7";    //sn密码

        String falg = "0";//失败
        String info = "";
        String msgCode = CreateRandom.createRandom(true, 6);    //生成验证码工具类
        String smsContent = "您好,尊敬的会员:您本次操作的验证码为:"+msgCode+",请及时验证!【金融猫大学生分期商城】";

       
        try {
      调用发送方法:
            SendMessageUtil.sendSMS(url, sn, pwd, phone, smsContent, request, response);
            falg = "1";
            info = "短信发送成功";
        } catch (Exception e)  {
            e.printStackTrace();
            info = "短信发送失败,请重新发送";
        }
       
        request.getSession().setAttribute("msgCode", msgCode);//session绑定验证码用于提交时的对比验证
       组装响应数据:
        Map<String, Object> codeMsgMap = new HashMap<String, Object>();
        codeMsgMap.put("status", falg);
        codeMsgMap.put("info", info);
        codeMsgMap.put("data", "");
        this.out(response, codeMsgMap);//调用输出方法
}

 

发送方法:
public static boolean sendSMS(String url,String sn,String pwd,String mobile,String content, HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException{
  response.setContentType("text/html;charset=UTF-8");
  content = URLEncoder.encode(content,"utf-8");
  String smsUrl = url+"sn="+sn+"&pwd="+pwd+"&mobile="+mobile+"&content="+content;//组装发送短信的完整数据
  URL getUrl;
  try { //准备
   getUrl = new URL(smsUrl);
   HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
          
     // 建立与服务器的连接,并未发送数据
            connection.connect();

            // 发送数据到服务器并使用Reader读取返回的数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                 System.out.println(lines);
           }
           reader.close();
           // 断开连接
           connection.disconnect();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  return true;
 }

输出方法:
 final protected void out(HttpServletResponse response, Object target) {
        GsonBuilder gbuild = new GsonBuilder();
        try {
            response.setContentType("text/javascript;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write(gbuild.create().toJson(target));
            out.close();
        } catch (IOException e) {
            throw new RuntimeException("Response writing failure.", e);
        }
    }


/**
   * 创建指定数量的随机字符串
   * @param numberFlag 是否是数字
   * @param length
   * @return
   */ 
  public static String createRandom(boolean numberFlag, int length){ 
   String retStr = ""; 
   String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz"; 
   int len = strTable.length(); 
   boolean bDone = true; 
   do { 
    retStr = ""; 
    int count = 0; 
    for (int i = 0; i < length; i++) { 
     double dblR = Math.random() * len; 
     int intR = (int) Math.floor(dblR); 
     char c = strTable.charAt(intR); 
     if (('0' <= c) && (c <= '9')) { 
      count++; 
     } 
     retStr += strTable.charAt(intR); 
    } 
    if (count >= 2) { 
     bDone = false; 
    } 
   } while (bDone); 
  
   return retStr; 
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics