1. 程式人生 > >(秒嘀)登錄短信驗證碼開發 Demo

(秒嘀)登錄短信驗證碼開發 Demo

log sep creat auth 獲取 new 數據 main httputil

註意:前提你要在秒嘀註冊: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();
}
}

(秒嘀)登錄短信驗證碼開發 Demo