【Java】通过调用阿里云短信服务给手机发短信
1.先到阿里云的云市场购买短信服务,一般会提供免费调用次数,大概十几二十次
2.下滑查看API接口详情
3.编写代码调用该接口
3.1 导入依赖
<!--阿里云sdk--><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.3</version></dependency><!--fastjson--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><!--httpClient--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><!--redis相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
3.2 接口配置(根据你购买的短信服务填写)
3.3 appcode在控制台复制
3.4 代码示例
Controller
/*** * @param sendCodeDTO (type 发送验证码类型 1-注册 2-登录,3-修改手机号)* @return*/@PostMapping("/sendCode")@ApiOperation("发送短信验证码")public CommonResult sendCode(@RequestBody SendCodeDTO sendCodeDTO) {return userService.sendCode(sendCodeDTO);}
SendCodeDTO
@Data
public class SendCodeDTO {/*** 手机号*/private String phone;/*** 验证码类型 : 1-注册,2-登录,3-修改手机号*/private Integer type;}
CodeTypeEnum(验证码类型枚举)
public enum CodeTypeEnum {/*** 验证码类型 : 1-注册,2-登录,3-修改手机号*/REGISTER(1,"REGISTER"),LOGIN(2,"LOGIN"),EDIT(3,"EDIT");public static String getDescriptionByCode(Integer code){for(CodeTypeEnum value : CodeTypeEnum.values()){if(value.getCode().equals(code)){return value.getDesc();}}return null;}CodeTypeEnum(Integer code, String desc) {this.code = code;this.desc = desc;}private final Integer code;private final String desc;public Integer getCode() {return code;}public String getDesc() {return desc;}}
(单纯发个短信验证码的话照着API文档的请求示例就可以实现了,需要根据验证码做处理的话,一般得存到Redis中)
HttpUtils
public class HttpUtils {/*** get** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doGet(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);HttpGet request = new HttpGet(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}/*** post form** @param host* @param path* @param method* @param headers* @param querys* @param bodys* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,Map<String, String> bodys)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (bodys != null) {List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();for (String key : bodys.keySet()) {nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));}UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");request.setEntity(formEntity);}return httpClient.execute(request);}/*** Post String** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}return httpClient.execute(request);}/*** Post stream** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exce