JAVA利用第三方平台发送短信验证码。

本文以注册为例,在SpringMVC+Spring+Mybatis框架的基础上完成该短信验证码功能。

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

         

复制代码

importjava.util.HashMap;

importorg.apache.commons.httpclient.Header;

importorg.apache.commons.httpclient.HttpClient;

importorg.apache.commons.httpclient.NameValuePair;

importorg.apache.commons.httpclient.methods.PostMethod;

 

importcom.yuetile.utils.VerifyingCodeGenerator;

publicclass SendMsg_webchineseController {

 

publicstatic HashMap<String,String>getMessageStatus(String phone)throwsException{

HashMap<String,String> m=newHashMap<String,String>();

HttpClient client = newHttpClient();

PostMethod post = newPostMethod("http://gbk.sms.webchinese.cn");

post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码

String code=VerifyingCodeGenerator.generate();//验证码

NameValuePair[] data ={ newNameValuePair("Uid", "****"),new NameValuePair("Key","******"),newNameValuePair("smsMob",phone),new NameValuePair("smsText","您正在注册本站会员,本次验证码为:"+code+""+"有效时间为5分钟")};

m.put("code", code);

post.setRequestBody(data);

client.executeMethod(post);

Header[] headers = post.getRespons                                         eHeaders();

intstatusCode = post.getStatusCode();

System.out.println("statusCode:"+statusCode);

for(Header h: headers)

{

System.out.println(h.toString());

}

String result = newString(post.getResponseBodyAsString().getBytes("gbk"));

System.out.println(result); //打印返回消息状态

m.put("result", result);post.releaseConnection();

return m;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送验证码,你需要使用服务提供商的 API。以下是一个使用阿里云服务的示例代码: ```java import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sms.model.v20170605.SendSmsRequest; import com.aliyuncs.sms.model.v20170605.SendSmsResponse; public class SmsSender { private static final String ACCESS_KEY_ID = "your_access_key_id"; private static final String ACCESS_KEY_SECRET = "your_access_key_secret"; private static final String SIGN_NAME = "your_sign_name"; private static final String TEMPLATE_CODE = "your_template_code"; public static void sendSms(String phone) throws ClientException { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com"); IAcsClient client = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setPhoneNumbers(phone); request.setSignName(SIGN_NAME); request.setTemplateCode(TEMPLATE_CODE); String code = String.format("%04d", new Random().nextInt(10000)); request.setTemplateParam("{\"code\":\"" + code + "\"}"); SendSmsResponse response = client.getAcsResponse(request); System.out.println("发送结果:" + response.getCode()); } } ``` 在上面的代码中,你需要替换以下常量: - `your_access_key_id`:你的阿里云 Access Key ID - `your_access_key_secret`:你的阿里云 Access Key Secret - `your_sign_name`:你在阿里云服务中设置的签名名称 - `your_template_code`:你在阿里云服务中设置的模板编号 使用时,你只需要调用 `sendSms` 方法并传入要发送的手机号码即可。该方法会生成一个 4 位的随机验证码并发送到指定的手机号码上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值