(秒嘀)登录短信验证码开发 Demo

注意:前提你要在秒嘀注册:http://www.miaodiyun.com/doc/demo.html

配置类   package com.miaodiyun.httpApiDemo.common;

public class Config

{
/**
* url前半部分
*/
public static final String BASE_URL = "https://api.miaodiyun.com/20150822";

/**
* 开发者注册后系统自动生成的账号,可在官网登录后查看
*/
public static final String ACCOUNT_SID = "56549525fc4**********013382bf973";

/**
* 开发者注册后系统自动生成的TOKEN,可在官网登录后查看
*/
public static final String AUTH_TOKEN = "a48b2*************ea19608a";

/**
* 响应数据类型, JSON或XML
*/
public static final String RESP_DATA_TYPE = "json";

}

发送类  package com.miaodiyun.httpApiDemo;

import com.miaodiyun.httpApiDemo.common.Config;
import com.miaodiyun.httpApiDemo.common.HttpUtil;

/**
 * 获取开发者账号信息接口调用示例
 * 
 * @ClassName: AccountInfo
 * @Description: 获取开发者账号信息接口调用示例
 *
 */
public class AccountInfo
{
private static String operation = "/query/accountInfo";
private static String accountSid = Config.ACCOUNT_SID;

/**
* 获取开发者账号信息
*/
public static void execute()
{
String url = Config.BASE_URL + operation;
String body = "accountSid=" + accountSid + HttpUtil.createCommonParam();

// 提交请求
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}
}

工具类 package com.miaodiyun.httpApiDemo.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * http请求工具
 */
public class HttpUtil
{
/**
* 构造通用参数timestamp、sig和respDataType

* @return
*/
public static String createCommonParam()
{
// 时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = sdf.format(new Date());

// 签名
String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);

return "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
}

/**
* post请求

* @param url
*            功能和操作
* @param body
*            要post的数据
* @return
* @throws IOException
*/
public static String post(String url, String body)
{
System.out.println("url:" + System.lineSeparator() + url);
System.out.println("body:" + System.lineSeparator() + body);

String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();

// 设置连接参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);

// 提交数据
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();

// 读取返回数据
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true; // 读第一行不加换行符
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}


/**
* 回调测试工具方法

* @param url
* @param reqStr
* @return
*/
public static String postHuiDiao(String url, String body)
{
String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();

// 设置连接参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);

// 提交数据
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();

// 读取返回数据
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true; // 读第一行不加换行符
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}

获取信息类   package com.miaodiyun.httpApiDemo;

import com.miaodiyun.httpApiDemo.common.Config;
import com.miaodiyun.httpApiDemo.common.HttpUtil;


/**
 * 获取开发者账号信息接口调用示例
 * 
 * @ClassName: AccountInfo
 * @Description: 获取开发者账号信息接口调用示例
 *
 */
public class AccountInfo
{
private static String operation = "/query/accountInfo";
private static String accountSid = Config.ACCOUNT_SID;
/**
* 获取开发者账号信息
*/
public static void execute()
{
String url = Config.BASE_URL + operation;
String body = "accountSid=" + accountSid + HttpUtil.createCommonParam();

// 提交请求
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}
}

测试类     package com.miaodiyun.httpApiDemo;

public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
// 获取开发者账号信息
AccountInfo.execute();
// 验证码通知短信接口
IndustrySMS.execute();
}
}

 

posted @ 2018-01-15 13:22  一璐到底  阅读(421)  评论(0编辑  收藏  举报