QHYun_practice-10

前面Redis连接搭好了,去redis的命令里或者客户端里把暂时测试的几个数据存进去,我在登录界面初始化的时候写了个函数,会吧用户名、密码等读出来自己填入(算是保存用户名密码的操作),不然测试阶段会疯掉。

现在就是需要写一个短信验证的服务,将验证码发给填入的手机号并且存入数据库,这样就可以进行一致性检验了。

首先我们要去叮咚注册一下,然后得到自己的apikey,然后要去申请一个验证码的模版,等待审批(不花钱有十条的试用,所以后面我就注释掉这个了,直接用redis的存值过)

还是去QHEntrance中建一个TelCodeCheck的包,然后建一个QHTelphoneCodeCheck的类。

package TelCodeCheck;
 
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/23 10:09
 */
public class QHTelphoneCodeCheck {
 
    private String phone ;
 
    public QHTelphoneCodeCheck(String tel) {
        // TODO 自动生成的构造函数存根
        this.phone = tel;
    }
 
    public  String TelCodeCheck()
    {
        //创建短信模板,将生成的验证码存到redis中
        String telRandomCode = SendMsgUtil.createRandomVcode();
        String content = "【启航云】您的验证码为" + telRandomCode + ",请在10分钟内输入。请勿告诉其他人!";
 
        //将验证码的短信发送出去
        try {
 
            content = java.net.URLEncoder.encode(content,"utf-8");
 
            System.out.println(content);
 
        } catch (UnsupportedEncodingException e) {
 
            // TODO Auto-generated catch block
 
            e.printStackTrace();
 
        }
        //短信接口URL提交地址
 
        String url = "http://api.dingdongcloud.com/v1/sms/sendyzm?apikey=cdb89eee84db80d8eccc8b291043fdb7&mobile="+this.phone+"&content="+content;
        HttpClient client = new HttpClient();
        //创建GET方法的实例
        GetMethod method = new GetMethod(url);
 
        //接收返回结果
        String result = null;
        try {
            //执行HTTP GET方法请求
            client.executeMethod(method);
 
            //返回处理结果
            result = method.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            //释放链接
            method.releaseConnection();
 
            //关闭HttpClient实例
            if (client != null) {
                ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
                client = null;
            }
            return telRandomCode;
        }
 
    }
}

建一个SendMsgUtil类

package TelCodeCheck;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
 
/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/23 10:15
 */
public class SendMsgUtil {
    /**
     * 发送短信消息
     * 方法说明
     * @Discription:扩展说明
     * @param phones
     * @param content
     * @return
     * @return String
     */
 
    @SuppressWarnings("deprecation")
 
    public static String sendMsg(String phones,String content) {
 
        try {
 
            content = java.net.URLEncoder.encode(content,"utf-8");
 
            System.out.println(content);
 
        } catch (UnsupportedEncodingException e) {
 
            // TODO Auto-generated catch block
 
            e.printStackTrace();
 
        }
 
        //短信接口URL提交地址
 
        String url = "http://api.dingdongcloud.com/v1/sms/sendyzm?apikey=cdb89eee84db80d8eccc8b291043fdb7&mobile="+phones+"&content="+content;
 
        Map params = new HashMap();
 
        params.put("zh", "账号");
 
        params.put("mm", "密码");
 
        params.put("dxlbid", "短信类别编号");
 
        params.put("extno", "扩展编号");
 
 
 
        //手机号码,多个号码使用英文逗号进行分割
 
        params.put("hm", phones);
 
        //将短信内容进行URLEncoder编码
 
        params.put("nr", URLEncoder.encode(content));
 
        return HttpRequestUtil.getRequest(url, params);
 
    }
 
    /**
     * 随机生成6位随机验证码
     * 方法说明
     * @Discription:扩展说明
     * @return
     * @return String
     */
 
    public static String createRandomVcode(){
 
        //验证码
 
        String vcode = "";
 
        for (int i = 0; i < 6; i++) {
 
            vcode = vcode + (int)(Math.random() * 9);
 
        }
 
        return vcode;
 
    }
 
    /**
     * 测试
     * 方法说明
     * @Discription:扩展说明
     * @param args
     * @return void
     */
 
    public static void main(String[] args) {
 
        // System.out.println(SendMsgUtil.createRandomVcode());
 
        // System.out.println("&ecb=12".substring(1));
        //System.out.println(sendMsg("15827429720", "【启航云】您的验证码为" + SendMsgUtil.createRandomVcode() + ",请在10分钟内输入。请勿告诉其他人!"));
        String content = "【启航云】您的验证码为" + SendMsgUtil.createRandomVcode() + ",请在10分钟内输入。请勿告诉其他人!";
        System.out.println(sendMsg("15827429720", content ));
    }
}

创建一个HttpRequestUtil的类

package TelCodeCheck;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
 
import java.io.IOException;
import java.util.Map;
 
/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/23 10:16
 */
public class HttpRequestUtil {
 
    public static String postRequest(String url, Map<String, String> params) {
        //构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
 
        //创建POST方法的实例
        PostMethod postMethod = new PostMethod(url);
 
        //设置请求头信息
        postMethod.setRequestHeader("Connection", "close");
 
        //添加参数
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postMethod.addParameter(entry.getKey(), entry.getValue());
        }
 
        //使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
 
        //接收处理结果
        String result = null;
        try {
            //执行Http Post请求
            httpClient.executeMethod(postMethod);
 
            //返回处理结果
            result = postMethod.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            //释放链接
            postMethod.releaseConnection();
 
            //关闭HttpClient实例
            if (httpClient != null) {
                ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                httpClient = null;
            }
        }
        return result;
    }
 
    /**
     * HttpClient 模拟GET请求
     * 方法说明
     * @Discription:扩展说明
     * @param url
     * @param params
     * @return String
     */
    public static String getRequest(String url, Map<String, String> params) {
        //构造HttpClient实例
        HttpClient client = new HttpClient();
 
        //拼接参数
        String paramStr = "";
        for (String key : params.keySet()) {
            paramStr = paramStr + "&" + key + "=" + params.get(key);
        }
        paramStr = paramStr.substring(1);
 
        //创建GET方法的实例
        GetMethod method = new GetMethod(url);
 
        //接收返回结果
        String result = null;
        try {
            //执行HTTP GET方法请求
            client.executeMethod(method);
 
            //返回处理结果
            result = method.getResponseBodyAsString();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            //释放链接
            method.releaseConnection();
 
            //关闭HttpClient实例
            if (client != null) {
                ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
                client = null;
            }
        }
        return result;
    }
}

去写一个测试函数,或者直接去外面的主程序里调用了TelCodeCheck这个函数,返回String存入Redis等校验的时候用,同时手机会收到短信,这样基本就OK了

posted @ 2025-07-21 09:30  Wind_Swing_Dunn  阅读(3)  评论(0)    收藏  举报