当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现发送短信验证码功能

Java实现发送短信验证码功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

赛道狂飙之国家永恒,永泰千江月休闲农场,荆门租房

一个发送短信验证码的功能,使用的是信易通的短信平台接口,然后在java中使用httpclient模拟post请求或者get请求(看短信平台要求,一般的情况下都是post请求),调用短信平台提供的接口(遵循短信平台的接口规范即可)。具体看代码:

使用httpclient的时候需要在项目中引入:

commons-httpclient-3.1.jar

这个jar包,

项目结构:

1、创建一个http的模拟请求工具类,然后写一个post方法或者get方法

/**
 * 文件说明
 * @description:扩展说明
 * @copyright: 2015 dreamtech.com.cn inc. all right reserved
 * @version: v6.0
 */
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;

/** 
 * @author: feizi
 * @date: 2015年4月17日 上午9:26:34 
 * @modifyuser: feizi
 * @modifydate: 2015年4月17日 上午9:26:34 
 * @version:v6.0
 */
public class httprequestutil {

 /**
  * httpclient 模拟post请求
  * 方法说明
  * @discription:扩展说明
  * @param url
  * @param params
  * @return string
  * @author: feizi
  * @date: 2015年4月17日 下午7:15:59
  * @modifyuser:feizi
  * @modifydate: 2015年4月17日 下午7:15:59
  */
 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
  * @author: feizi
  * @date: 2015年4月17日 下午7:15:28
  * @modifyuser:feizi
  * @modifydate: 2015年4月17日 下午7:15:28
  */
 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 + "?" + 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;
 }
}

2、在创建一个类,生成验证码,然后传递相应的参数(不同的短信平台接口会有不同的参数要求,这个一般短信平台提供的接口文档中都会有的,直接看文档然后按要求来即可)

/**
 * 文件说明
 * @description:扩展说明
 * @copyright: 2015 dreamtech.com.cn inc. all right reserved
 * @version: v6.0
 */
package com.demo.util;

import java.net.urlencoder;
import java.util.hashmap;
import java.util.map;

/** 
 * @author: feizi
 * @date: 2015年4月17日 上午9:24:48 
 * @modifyuser: feizi
 * @modifydate: 2015年4月17日 上午9:24:48 
 * @version:v6.0
 */
public class sendmsgutil {

 /**
  * 发送短信消息
  * 方法说明
  * @discription:扩展说明
  * @param phones
  * @param content
  * @return
  * @return string
  * @author: feizi
  * @date: 2015年4月17日 下午7:18:08
  * @modifyuser:feizi
  * @modifydate: 2015年4月17日 下午7:18:08
  */
 @suppresswarnings("deprecation")
 public static string sendmsg(string phones,string content){
  //短信接口url提交地址
  string url = "短信接口url提交地址";

  map<string, string> params = new hashmap<string, string>();

  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
  * @author: feizi
  * @date: 2015年4月17日 下午7:19:02
  * @modifyuser:feizi
  * @modifydate: 2015年4月17日 下午7:19:02
  */
 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
  * @author: feizi
  * @date: 2015年4月17日 下午7:26:36
  * @modifyuser:feizi
  * @modifydate: 2015年4月17日 下午7:26:36
  */
 public static void main(string[] args) {
//  system.out.println(sendmsgutil.createrandomvcode());
//  system.out.println("&ecb=12".substring(1));
  system.out.println(sendmsg("18123456789,15123456789", "尊敬的用户,您的验证码为" + sendmsgutil.createrandomvcode() + ",有效期为60秒,如有疑虑请详询400-069-2886(客服电话)【xxx中心】"));
 }
}

然后执行一下,一般的情况下参数传递正确,按照接口文档的规范来操作的话,都会发送成功的,手机都能收到验证码的,然后可能会出现的问题就是:发送的短信内容有可能会出现中文乱码,然后就会发送不成功,按照短信平台的要求进行相应的编码即可。一般都会是utf-8编码。

完整代码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网