HttpClient发送短信案例

@GetMapping("/phone/send/code")
@ApiOperation(value = "发送短信验证码", notes = "发送短信验证码 的详细描述")
public Result sendCode(@RequestParam("phone") String phone) {
    if (!CommonUtil.checkPhone(phone)) return Result.fail("手机号码格式错误");
    if (userService.queryByPhone(phone) != null) return Result.fail("手机号码已注册");

    // post请求 参数用query
    String host = "https://gyytz.market.alicloudapi.com/sms/smsSend";
    // 密钥
    String appcode = "你的密钥";
    // 短信模板 网上有模板
    String templateId = "a09602b817fd47e59e7c6e603d3f088d";
    // 短信前缀 例如:【阿里云】
    String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";
    // 验证码:1234 2分钟内有效
    String param = "**code**:1234,**minute**:2";

    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建http POST请求
    HttpPost httpPost = new HttpPost(host);

    // 请求头
    httpPost.setHeader("Authorization", "APPCODE " + appcode);

    //#region 请求体
    List<NameValuePair> querys = new ArrayList<>();
    querys.add(new BasicNameValuePair("mobile", phone));
    querys.add(new BasicNameValuePair("templateId", templateId));
    querys.add(new BasicNameValuePair("smsSignId", smsSignId));
    querys.add(new BasicNameValuePair("param", param));
    // 构造一个form表单式的实体
    UrlEncodedFormEntity formEntity;
    try {
        formEntity = new UrlEncodedFormEntity(querys);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    httpPost.setEntity(formEntity);
    //#endregion

    CloseableHttpResponse response = null;
    try {
        // 执行请求
        response = httpclient.execute(httpPost);

        // 判断返回状态是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            // 自定义返回状态码
            if (jsonObject.getIntValue("code") == 0) {
                // 发送成功
                return Result.success("短信已发送,2分钟内有效,请注意查收!");
            } else {
                return Result.fail("短信发送失败:" + jsonObject.getString("msg"));
            }
        } else {
            return Result.fail(response.getStatusLine().getReasonPhrase());
        }
    } catch (IOException e) {
        return Result.fail("短信发送失败:" + e.getMessage());
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋劲豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值