当前位置: 代码迷 >> Java Web开发 >> jsp 短信验证码的demo啊该怎么解决
  详细解决方案

jsp 短信验证码的demo啊该怎么解决

热度:3916   发布时间:2016-04-10 23:04:44.0
jsp 短信验证码的demo啊
求一个jsp开发 短信验证码的例子

------解决方案--------------------
关键得有短信猫
------解决方案--------------------
不同的短信平台,方法不一样,不过为了方便接入,肯定都是很简单的
------解决方案--------------------

    public static String SMS(String phone) {
        try {
            //发送POST请求
         long yzCode= Math.round(Math.random()*899999+100000);
          ServletActionContext.getRequest().getSession().setAttribute("phonevalcode", yzCode);
           // URL url = new URL("http://utf8.sms.webchinese.cn");
            URL url = new URL("http://106.ihuyi.com/webservice/sms.php?method=Submit");
            String postData="account=cf_hehehe&password=hehe123&mobile="+java.net.URLEncoder.encode(phone,"utf-8")+"&content="+java.net.URLEncoder.encode("您的验证码是:"+yzCode+"。请不要把验证码泄露给其他人。","utf-8");
           //String postData="Uid=yonjue&Key=hehe1234567890&smsMob="+java.net.URLEncoder.encode(phone,"utf-8")+"&smsText="+java.net.URLEncoder.encode("尊敬的用户,您在线下100注册验证码是:"+yzCode+"。请不要把验证码泄露给其他人","utf-8");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }
    

------解决方案--------------------
引用:
  相关解决方案