短信服务(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. [Swift]LeetCode40. 组合总和 II | Combination Sum II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  2. [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  3. [Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. 解决同一页面中两个iframe互相调用jquery,js函数

    这一个月又没更新博客,唉,懒癌又犯了,今天解决了一个问题,关于两个iframe互相调用jquery函数方法 a.html中有两个iframe,如下: <iframe width="10 ...

  5. 【异常】Servlet.service() for servlet [springMvc] in context with path [/orderdishessystem] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: net/sf/ezmorph/M

    今天做登录的时候,引入json-lib-2.1-jdk15.jar的包时,执行到JSONObject jsonObject = new JSONObject()对象就报标题的那个错. 原来是除了要导入 ...

  6. 上下div高度动态自适应--另类处理方案

    这段时间在工作中遇到一个看似较为棘手的问题.问题描述:查询报表页面分为上下两部分,上部分为条件输入区域,下部分为报表展示区域.客户要求做到默认满屏(但要动态适应不同的窗体大小,也就是浏览器窗体用户会手 ...

  7. AspNetCore 使用log4net+IExceptionFilter 记录错误日志

    错误日志的好处我就不说了,大家都心里有数,那今天浩子就给大家说一说基本的错误日志吧这次通过log4net记录日志. 原来写过一个关于Nlog的日志框架,传送门为:https://www.cnblogs ...

  8. SignalR学习笔记(五) 横向扩展之SQL Server

    当一个Web应用程序达到一台服务器能力限制,即请求处理数量限制之后,有2种解决方案:纵向扩展和横向扩展. 纵向扩展即用更强的服务器(或虚拟机),或为当前的服务器添加更多的内存,CPU等 横向扩展即添加 ...

  9. Eclipse导入别人的项目报错:Unable to load annotation processor factory 'xxxxx.jar' for project

    使用eclipse导入别人的项目时候,报错Unable to load annotation processor factory 'xxxxx.jar' for project. 解决方案 1.项目右 ...

  10. SQL Server查询所有的表名、字段名、注释

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...