Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to send SMS CAPTCHA by java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要介绍了java如何实现发送短信验证码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

最近用学习了一下调用第三方接口发送短信验证码的程序,希望能够帮助到大家。

1.首先下图为项目的目录结构,需要带入三个包:

commons-httpclient-3.1.jar

commons-logging-1.0.4.jar

codec-1.3.jar

2.其次要创建模拟POST、GET请求的工具类:

package com.demo.util; import java.io.IOException;import java.util.Map; 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; public class HttpRequestUtil { /** * HttpClient 模拟POST请求 * @param url * @param params * @return */ public static String postRequest(String url, Map params) { //构造HttpClient的实例 HttpClient httpClient = new HttpClient(); //创建POST方法的实例 PostMethod postMethod = new PostMethod(url); //设置请求头信息 postMethod.setRequestHeader("Connection", "close"); //添加参数 for (Map.Entry 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 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 + "?" + paramStr); //接收返回结果 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; }}

3.创建发送短信的方法,其中要通过短信内容要进行设置编码集为utf-8,调用第三方接口传参要按照第三方文档规范:

package com.demo.util; import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map; 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 = "https://api.dingdongcloud.com/v1/sms/sendyzm?apikey=b46c4961aa875f626b7924aace0d53f7&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("18201150549", "【签名】尊敬的用户,您的验证码为" + SendMsgUtil.createRandomVcode() + ",请在10分钟内输入。请勿告诉其他人!")); }}

4.调用main方法可以进行测试,如果控台太输出返回状态值不是200,可以参考第三方发短信的文档返回参数说明。

感谢你能够认真阅读完这篇文章,希望小编分享的"java如何实现发送短信验证码"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report