package com.csy.cn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
@WebServlet("/getCode")
public class Sendcode extends HttpServlet{
private static final String QUERY_URL="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
private static final String ACCOUNT_SID="d8ad4b0a021e4747ac227d3642efaeeb";
private static final String AUTH_TOKEN="7632bbb90a9744f28cee049ea23476f6";
public static final String RESP_DATA_TYPE = "json";
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String phone=request.getParameter("phone");
//发送验证码给前台
String code=sendcode(phone);
try {
response.getWriter().print(code);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取验证码
public static String sendcode(String phone) {
String code=getRandomCode();//随机码
String smsContent="【谭州JAVA学院】"+"您的验证码为"+code+",请于分钟内正确输入,如非本人操作,请忽略此短信。";
String tmpSmsContent = null;
try{
tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
}catch(Exception e){
}
String url = QUERY_URL;
String body = "accountSid=" + ACCOUNT_SID + "&to=" + phone + "&smsContent=" + tmpSmsContent
+ createCommonParam();
// 提交请求
String result = post(url, body);
// System.out.println("result:" + System.lineSeparator() + result+"pppp");
JSONObject object;
try {
object = new JSONObject(result);
String responeCode=object.getString("respCode");
System.out.println("responeCode////"+responeCode);
if (responeCode.equals("00000")) {
return code;
}else {
return "nonono";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return code;
}
//获取随机数
public static String getRandomCode(){
String ranNum=new Random().nextInt(1000000)+"";
if (ranNum.length()!=6) {
getRandomCode();
}
return ranNum;
}
//获取时间
public static String createCommonParam()
{
// 时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = sdf.format(new Date());
// 签名
String sig = DigestUtils.md5Hex(ACCOUNT_SID + AUTH_TOKEN + timestamp);//加密
return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + 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);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 提交数据
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;
}
@Test
public void sendcode(){
System.out.println(sendcode("13609532983"));
}
}