当前位置:网站首页>云片网案例
云片网案例
2022-08-02 14:01:00 【昊天h1】
一, 导入依赖
<!--云片网-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
二,导入工具类
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * @auther Teao * @description 手机短信API **/
public class JavaSmsApi {
//智能匹配模版发送接口的http地址
private static String URI_SEND_SMS = "地址";
//编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8";
// private static String apikey = "apikey";
public static String sendSms(String mobile,String code) throws IOException {
mobile=URLEncoder.encode(mobile,ENCODING);
String text = "短信模板信息";
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
// 这里返回的是Json字符串
return post(URI_SEND_SMS, params);
}
/** * 基于HttpClient 4.3的通用POST方法 * * @param url 提交的URL * @param paramsMap 提交<参数,值>Map * @return 提交响应 */
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(responseText);
return responseText;
}
}
三,使用
1.发送验证码
/** * @description:发送验证码 * @author: Teao * @date: 2022/7/16 14:31 * @param: [phoneNumber] * @return: com.teao.springboot003.entity.R **/
@GetMapping("/sendCode")
@ApiOperation("发送验证码")
public R sendCode(String phoneNumber) throws IOException {
// 创建一个4位数的随机整数作为验证码
String code = RandomUtil.randomNumbers(4);
// 调用工具类发送验证码,用sms接收JSON字符串
String sms = JavaSmsApi.sendSms(phoneNumber, code);
// JSON字符串转成map方便取值
JSONObject map = JSONUtil.parseObj(sms);
// 取返回的code
String responseCode = map.get("code").toString();
// 判断返回的状态码是不是0 是发送成功,不是发送失败
if (responseCode.equals("0")){
// 发送成功后将 key为手机号,value为验证码,推送到redis存储,有效期1分钟
redisTemplate.opsForValue().set(phoneNumber,code,120,TimeUnit.SECONDS);
return new R(1000,"验证码发送成功", code);
}
return new R(-1000,"验证码发送失败", code);
}
2.注册或者登录
/** * @description:用户手机注册 * @author: Teao * @date: 2022/7/16 14:32 * @param: [phoneNumber, code] * @return: com.teao.springboot003.entity.R **/
@GetMapping("/reg")
@ApiOperation("用户手机注册")
public R reg(String phoneNumber,String code) throws IOException{
// redisTemplate.hasKey(phoneNumber) 判断验证码是否失效 ()
if (!redisTemplate.hasKey(phoneNumber)){
return new R(-1000,"验证码已过期", null);
}
// 验证码没过期时 获取到第三方服务存储在redis上的验证码
String c = redisTemplate.opsForValue().get(phoneNumber).toString();
// 将第三方的与自己输入的进行比较
if (!c.equals(code)){
return new R(-1000,"验证码错误", null);
}
return new R(1000,"注册成功", null);
}
边栏推荐
- idea社区版下载安装教程_安装天然气管道的流程
- Based on the flask mall administrator functions
- 电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)
- Selenium本地打开远程浏览器
- 你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
- 政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
- Break the limit of file locks and use storage power to help enterprises grow new momentum
- 网络安全第三次作业
- Detailed explanation of stored procedures
- C# using 使用方法
猜你喜欢

binary search && tree

Supervision strikes again, what about the market outlook?2021-05-22

Break the limit of file locks and use storage power to help enterprises grow new momentum

MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

巴比特 | 元宇宙每日必读:蒂芙尼宣布推出限量版 CryptoPunk 定制吊坠

鲲鹏devkit & boostkit

Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush

面试SQL语句,学会这些就够了!!!

ZABBIX配置邮件报警和微信报警
![[C language] Analysis of function recursion (2)](/img/b1/9baa60a69d41a1823ed92001d1b0a0.png)
[C language] Analysis of function recursion (2)
随机推荐
无序数组排序并得到最大间隔
Selenium本地打开远程浏览器
【ONE·Data || Getting Started with Sorting】
腾讯安全游戏行业研讨会:生态共建,护航游戏产业健康发展
Supervision strikes again, what about the market outlook?2021-05-22
专访|带着问题去学习,Apache DolphinScheduler 王福政
Shell脚本完成pxe装机配置
第二讲 软件生命周期
hsql是什么_MQL语言
MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
RKMPP库快速上手--(一)RKMPP功能及使用详解
WeChat Mini Program-Recent Dynamic Scrolling Implementation
未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
数据机构---第六章图---图的遍历---选择题
如何解决mysql服务无法启动1069
About the development forecast of the market outlook?2021-05-23
Word | 关于删除分节符(下一页)前面的版式就乱了解决方案
【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'
软件测试和硬件测试的区别及概念
C# 编译错误:Compiler Error CS1044