zoukankan      html  css  js  c++  java
  • 第三方短信接口使用测试

    1、导入包:

    // https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient
    compile group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
    

    2、选一个第三方短信网站
    http://sms.webchinese.cn/
    3、了解其中短信测试接口
    4、测试代码如下:

        import org.apache.commons.httpclient.Header;
        import org.apache.commons.httpclient.HttpClient;
        import org.apache.commons.httpclient.NameValuePair;
        import org.apache.commons.httpclient.methods.PostMethod;
    
        public static void main(String[] args) {
                try {
                    String code = sendCode("your phone","xxx","xx");
                    System.out.println( code );
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    public static String sendCode(String phone,String content,String code)throws Exception{
        System.out.println("::::::::::::"+code);
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://utf8.api.smschinese.cn");
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码
        NameValuePair[] data ={ new NameValuePair("Uid", "注册的用户名"),
                new NameValuePair("Key", "注册后的密钥"),
                new NameValuePair("smsMob",phone),
                new NameValuePair("smsText",content)};
        post.setRequestBody(data);
    
        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:"+statusCode);
        for(Header h : headers)
        {
            System.out.println(h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));//或gbk
        System.out.println(result); //打印返回消息状态
        post.releaseConnection();
        return result;
    }
    

    另外:
    若是用于短信注册或登录验证,原理如下:
    发送短信验证码的原理是:随机生成一个6位数字,将该6位数字保存到session当中,客户端通过sessionid判断对应的session,用户输入的验证码再与session记录的验证码进行比较。

  • 相关阅读:
    MySQL的count函数注意点
    case when语句的报错问题
    redis的主从搭建与sentinel高可用服务的搭建
    解析范式(1NF-4NF)
    对SQL语言的相关学习
    ASP.NET Core MVC+EF Core项目实战
    ASP.NET Core +Highchart+ajax绘制动态柱状图
    tab页卡效果!
    今天我注册了迅雷快传
    触发器学习笔记(:new,:old用法)
  • 原文地址:https://www.cnblogs.com/heavenTang/p/7472429.html
Copyright © 2011-2022 走看看