短信服务(Short Message Service)是网易网易云通信为用户提供的一种通信服务的能力,目前支持验证码类短信、通知类短信、运营类短信、语音类短信、国际短信等事务性短信。网易网易云通信短信功能具体有全网覆盖、3-5 秒可达、超高到达率、7*24 小时服务监控等优势。按量付费、阶梯定价,发送越多单价越低。API调用简单,加快接入速度。

我们这里主要介绍使用OkHttp和Retrofit来做一些请求,就不做介绍了,直接使用代码来注释。

OkHttp

private final static String vercodeserverurl = "https://api.netease.im/sms/sendcode.action";
private final static String vercodeappkey = "5970a1e************46ae";
private final static String vercodeappsecret = "5*****4";
private final static String vercodetemplateid = "30******7"; public static void main(String[] args) throws IOException {
String nonce = ((int) (Math.random() * 100000)) + "";
String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(vercodeappsecret, nonce, curTime); FormBody.Builder builder = new FormBody.Builder();
builder.add("mobile", "155********");
RequestBody formBody = builder.build(); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder()
.url(vercodeserverurl)
.addHeader("AppKey",vercodeappkey)
.addHeader("Nonce",nonce)
.addHeader("CurTime",curTime)
.addHeader("CheckSum",checkSum)
.post(formBody)
.build();
okhttp3.Response execute = client.newCall(request).execute();
ResponseBody body = execute.body();
System.out.println(body.string());
System.out.println("完成");

Retrofit

接口声明

public interface WebInterface {

    @FormUrlEncoded
@POST("sms/sendcode.action")
Call<MessageResponse> sendMessage(@Header("AppKey") String apiKey,
@Header("Nonce") String Nonce,
@Header("CurTime") String CurTime,
@Header("CheckSum") String CheckSum,
@Field("mobile") String mobile);
}

返回实体封装

public class MessageResponse {

    private int code;
private String msg;
private String obj; //省略全参构造 @Override
public String toString() {
return "MessageResponse{" +
"code=" + code +
", msg='" + msg + '\'' +
", obj='" + obj + '\'' +
'}';
}
}

Retrofit调用接口

public class SendMessage {

    private final static String vercodeserverurl="https://api.netease.im/";
private final static String vercodeappkey="5970a1*************46ae";
private final static String vercodeappsecret="5***********4";
private final static String vercodetemplateid="3******7";
private final static String content="application/x-www-form-urlencoded;charset=utf-8"; public static void main(String[] args) throws IOException {
String nonce = ((int) Math.random() * 100000) + "";
String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(vercodeappsecret, nonce, curTime); Retrofit retrofit = new Retrofit.Builder()
.baseUrl(vercodeserverurl) //设置网络请求的Url地址
.addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
.build(); WebInterface webInterface = retrofit.create(WebInterface.class);
Call<MessageResponse> messageResponseCall = webInterface.sendMessage(vercodeappkey,
nonce,
curTime,
checkSum,
//content,
"155*********");
//同步执行
Response<MessageResponse> execute = messageResponseCall.execute();
MessageResponse messageResponse= execute.body();
//判断是否成功等代码省略
}
}

使用OkHttp和Retrofit发送网易云信验证码的更多相关文章

  1. 网易云信,发送验证码短信C#版代码

    网易云信发送短信代码(C# 版)....需要注意SHA1 String有转换小写!!!! using System; using System.Collections.Generic; using S ...

  2. php对接网易云信视频直播

    <?php/** * Created by PhpStorm. * User: lhl * Date: 2019/4/10 * Time: 17:31 */ namespace app\api\ ...

  3. 模板短信接口调用java,pythoy版(一) 网易云信

    说明 短信服务平台有很多,我只是个人需求,首次使用,算是测试用的,故选个网易(大公司). 稳定性:我只测试了15条短信... 不过前3条短信5分钟左右的延时,后面就比较快.... 我只是需要发短信,等 ...

  4. 子弹短信光鲜的背后:网易云信首席架构师分享亿级IM平台的技术实践

    本文原文内容来自InfoQ的技术分享,本次有修订.勘误和加工,感谢原作者的分享. 1.前言 自从2018年8月20日子弹短信在锤子发布会露面之后(详见<老罗最新发布了“子弹短信”这款IM,主打熟 ...

  5. 微信小程序开发中的二三事之网易云信IMSDK DEMO

    本文由作者邹永胜授权网易云社区发布. 简介 为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程.用产品的话说就是: 云信 IM 小程序 SDK 的能力 ...

  6. 网易云信技术分享:IM中的万人群聊技术方案实践总结

    本文来自网易云信团队的技术分享,原创发表于网易云信公众号,原文链接:mp.weixin.qq.com/s/LT2dASI7QVpcOVxDAsMeVg,收录时有改动. 1.引言 在不了解IM技术的人眼 ...

  7. 网易云信-新增自定义消息(iOS版)

    https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...

  8. 网易云信 QUIC 加速服务架构与实践

    导语:网易云信作为音视频服务提供商的领导者,一直致力于提供顶级的音视频通话服务体验,为用户在各种恶劣环境下提供可靠的音视频服务.如何在极端弱网条件下仍然能给用户提供可靠的音视频服务,是网易云信关注的重 ...

  9. 用Retrofit发送请求中添加身份验证

    用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...

随机推荐

  1. #Java学习之路——面试题

    (一)[基础知识梳理——JAVAse部分]Java中的变量和常量        在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改 ...

  2. [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  3. [Swift]LeetCode685. 冗余连接 II | Redundant Connection II

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  4. AES,DES加密JS源文件及其使用方法

    源文件地址:https://github.com/dididi1234/crypto 进入之后直接下载CryptoJS.js,js中直接引用,小程序也一样可以使用 具体使用方法和vue中的Crypto ...

  5. 聊聊Python的time模块

    time模块 time模块是很多人最早接触到的模块,像time.sleep(x)好像随处可见,但是time模块里面的其他方法呢?下面一起看一下time模块. 在Python中一般用这3种方式表示时间: ...

  6. .net core consul 服务配置 服务发现 服务健康检测 服务变更加载

    准备环境 安装consul之后 1. 创建一个.net core webapi 举例为UsercenterService 2. nuget引用Consul组件  https://github.com/ ...

  7. Filebeat 模块与配置

    续 • <开始使用Filebeat> 1.  关于Filebeat 当你要面对成百上千.甚至成千上万的服务器.虚拟机和容器生成的日志时,请告别 SSH 吧!Filebeat 将为你提供一种 ...

  8. InstallShield Limited Edition Project 打包windows服务解析

    最近项目从vs2005 升级到vs2010后,发现新的vs2010 不再带有原来的安装工程项目,导致以前的安装包不可以使用,查找资料后发现微软从vs2010 版本后不再提供自带的安装工程,尝试着利用  ...

  9. .net core使用EasyNetQ做EventBus

    随着SOA.微服务.CQRS的盛行,EventBus越来越流行,上GitHub搜了一下,还是有蛮多的这类实现,老牌的有NServiceBus(收费).MassTransit,最近的有CAP(国人写的, ...

  10. Unity 本地坐标到世界坐标,世界坐标到本地坐标

    世界=>本地 public GameObject mTarget; public GameObject mPar; //这个注意一定要是mTarget的第一父物体. // Use this fo ...