当前位置:网站首页>云片网案例
云片网案例
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);
}
边栏推荐
- 【ONE·Data || Getting Started with Sorting】
- Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
- 巴比特 | 元宇宙每日必读:蒂芙尼宣布推出限量版 CryptoPunk 定制吊坠
- 【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
- VMM是什么?_兮是什么意思
- Some impressions of the 519 plummet 2021-05-21
- AWVS工具介绍[通俗易懂]
- hsql是什么_MQL语言
- els strip collision deformation judgment
- 社区收藏缓存设计重构实战
猜你喜欢

科研试剂DSPE-PEG-VIP,二硬脂酰基磷脂酰乙醇胺-聚乙二醇-血管活性肠肽VIP

CVE-2020-27986(Sonarqube敏感信息泄漏) 漏洞修复
![Embedded system driver primary [2] - based on character device driver _ basic framework](/img/71/4feb9e42fd2ff1bc4a2d49798657b2.png)
Embedded system driver primary [2] - based on character device driver _ basic framework

专访|带着问题去学习,Apache DolphinScheduler 王福政

WeChat Mini Program-Recent Dynamic Scrolling Implementation

uview 2.x版本 tabbar在uniapp小程序里头点击两次才能选中图标

拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢

Object detection scene SSD-Mobilenetv1-FPN

About the development forecast of the market outlook?2021-05-23

【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'
随机推荐
如何解决mysql服务无法启动1069
【ONE·Data || Getting Started with Sorting】
CVE-2020-27986(Sonarqube敏感信息泄漏) 漏洞修复
SQL函数 TRUNCATE
ttl电平与rs232电平转换电路(232电平定义)
【学习笔记】数位dp
C language improvement (3)
C# using 使用方法
线代:已知一个特征向量快速求另外两个与之正交的特征向量
replay视频播放器_怎么让手机音乐跟视频一起放
方舟生存进化淘宝面板服务器是怎么一回事?
WiFi Association&Omnipeek抓包分析
腾讯安全游戏行业研讨会:生态共建,护航游戏产业健康发展
Image retrieval method based on deep learning!
Word | 关于删除分节符(下一页)前面的版式就乱了解决方案
第二讲 软件生命周期
为什么四个字节的float表示的范围比八个字节的long要广
配置zabbix自动发现和自动注册。
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
deal!It's July 30th!