发表于: 2018-03-27 23:43:19
2 586
今日完成:
1. jdbc插入获取自增id
2. reader和inputStream
java.io下面有两个抽象类:InputStream和Reader
InputStream是表示字节输入流的所有类的超类
Reader是用于读取字符流的抽象类
InputStream提供的是字节流的读取,而非文本读取,这是和ReatStreamder类的根本区别。
即用Reader读取出来的是char数组或者String ,使用InputStream读取出来的是byte数组。
3. 注册认证荣联,SendCloud和金山云账号并通过认证
4. 学习荣联发送短信验证码的机制和Demo,使用荣联平台进行配置
验证流程:
源码通过使用传入的参数:官网上的开发者账号和应用信息,构造一个https请求:
public HashMap<String, Object> QuerySMSTemplate(String templateId) {
HashMap<String, Object> validate = accountValidate();
if (validate != null)
return validate;
CcopHttpClient chc = new CcopHttpClient();
DefaultHttpClient httpclient = null;
try {
httpclient = chc.registerSSL(SERVER_IP, "TLS",
Integer.parseInt(SERVER_PORT), "https");
} catch (Exception e1) {
e1.printStackTrace();
LoggerUtil.error(e1.getMessage());
throw new RuntimeException("初始化httpclient异常" + e1.getMessage());
}
String result = "";
try {
HttpPost httppost = (HttpPost) getHttpRequestBase(1,
Query_SMSTemplate);
String requsetbody = "";
if (BODY_TYPE == BodyType.Type_JSON) {
JsonObject json = new JsonObject();
json.addProperty("appId", App_ID);
json.addProperty("templateId", templateId);
requsetbody = json.toString();
} else {
requsetbody = "<?xml version='1.0' encoding='utf-8'?><Request>"
+ "<appId>" + App_ID + "</appId>" + "<templateId>"
+ templateId + "</templateId>" + "</Request>";
}
LoggerUtil.info("QuerySMSTemplate Request body = " + requsetbody);
//打印包体
System.out.println("请求的包体:"+requsetbody);
BasicHttpEntity requestBody = new BasicHttpEntity();
requestBody.setContent(new ByteArrayInputStream(requsetbody
.getBytes("UTF-8")));
requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
httppost.setEntity(requestBody);
HttpResponse response = httpclient.execute(httppost);
//获取响应码
status = response.getStatusLine().getStatusCode();
System.out.println("Https请求返回状态码:"+status);
HttpEntity entity = response.getEntity();
可以概括为执行下面两行代码:构造http请求体,使用https发送,并通过response接收返回的数据
HttpResponse response = httpclient.execute(httppost);
//获取响应码
status = response.getStatusLine().getStatusCode();
通过返回的数据判断短信发送状态:
if("000000".equals(result.get("statusCode"))){
//正常返回输出data包体信息(map)
HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
Set<String> keySet = data.keySet();
for(String key:keySet){
Object object = data.get(key);
System.out.println(key +" = "+object);
}
测试结果:
明日计划:
1. 使用xml文件配置荣联,实现用户验证码注册和登陆
2. 实现邮件
3. 学习金山云存储图片
遇到的问题:
1. 暂无
收获:
1. 使用荣联发送验证码,理解实现的大体过程
评论