Java实现手机短信验证码(互亿无线)

互亿无线

互亿无线是一家提供电信类增值服务插件以及其他相关插件的公司,是中国移动、中国联通、中国电信三大运营商的战略合作伙伴与工信部认定的电信增值业务服务商。公司旗下运营三大业务平台:数字奖励营销活动平台、应用短信平台、营销短信平台。

官网:短信api_短信接口api_短信接口验证码_短信验证码API_短信通知api接口_互亿无线

实现方法

1、pom.xml文件导包

XML 复制代码
        <!--    短信    --> 
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
 
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

2、创建发送短信工具类

一般把这个类放到util工具目录下(我自己是在若依框架下的项目)

java 复制代码
package com.ruoyi.common.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;


public class SendSmsUtil {
    private static final String URL = "http://106.ihuyi.com/webservice/sms.php?method=Submit"; //国内请求路径
    private static final String APPID = "******(这里填写自己的APPID)";
    private static final String APIKEY = "*****(这里填写自己的APIKEY)";

    public static String SMS(String number) throws HttpException, IOException{

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(URL);

        client.getParams().setContentCharset("utf-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=utf-8");
        //随机编号
        int mobile_code = (int)((Math.random()*9+1)*100000);

        String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");

        NameValuePair[] data = {//提交短信
                new NameValuePair("account",APPID ), //查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
                new NameValuePair("password", APIKEY), //查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
                new NameValuePair("mobile", number),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            String SubmitResult =method.getResponseBodyAsString();


            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println("code"+code);
            System.out.println("msg"+msg);
            System.out.println("smsid"+smsid);
            System.out.println("mo"+mobile_code);

            if("2".equals(code)){
                System.out.println("短信提交成功");
                System.out.println(mobile_code);
                return mobile_code+"";
            }

        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
}

注意!!!

在写工具类之前,要先去互亿无线官网注册账号,获取到你个人的一个APPID和APIKEY。

3、Contriller控制层调用工具类

java 复制代码
//获取验证码
@RequestMapping("/sendCode")
    public void sendCode(HttpServletRequest req, PrintWriter pw)
            throws ServletException, IOException {
        String number=req.getParameter("number");
        String strCode= SendSmsUtil.SMS(number);
        req.getSession().setAttribute("code", strCode);
        System.out.println("strCode"+strCode);
        if(strCode!=""){
            pw.write("success");
        }
    }

4、前端代码

html 复制代码
//界面显示
电话号码: <input type="text" id="number" name="number"  placeholder="电话" />
验证码: <input type="text" id="captcha" name="captcha"   />
<input type="button" value="获取验证码" id="update" onclick="sendSms();"/>
javascript 复制代码
function sendSms() {
				var number = $("#number").val();
				$.post("/html/sendCode",{number:number},function (data) {
					if (data == "success"){
						var second = 60;
						console.log("发送成功");
					}
				})
			}

可以参考这两篇文章:

Java实现手机发送短信验证码_java发送短信验证码-CSDN博客

Java短信验证码-互亿无线_互亿无线 工具类-CSDN博客

相关推荐
侠客行031715 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪15 小时前
深入浅出LangChain4J
java·langchain·llm
子兮曰15 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖15 小时前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神15 小时前
github发布pages的几种状态记录
前端
灰子学技术17 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
老毛肚17 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎17 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
二十雨辰17 小时前
[python]-AI大模型
开发语言·人工智能·python
不像程序员的程序媛18 小时前
Nginx日志切分
服务器·前端·nginx