package com.aaa.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import org.json.JSONObject; /** * * @Title:GetMessageCode * @Description:发送验证码 * @author:罗雨 * @Date 2017年12月10日 下午10:03:04 */ public class GetMessageCode { private static final String QUERY_PATH="https://api.miaodiyun.com/20150822/industrySMS/sendSMS"; private static final String ACCOUNT_SID="b851ebba32564896978ad9b42ec3d483"; private static final String AUTH_TOKEN="053703f4568a46b5bffca4343c40511d"; //根据相应的手机号发送验证码 public static String getCode(String phone){ String rod=smsCode(); String timestamp=getTimestamp(); String sig=getMD5(ACCOUNT_SID,AUTH_TOKEN,timestamp); String tamp="【罗雨网络】登录验证码:"+rod+",如非本人操作,请忽略此短信。"; OutputStreamWriter out=null; BufferedReader br=null; StringBuilder result=new StringBuilder(); try { URL url=new URL(QUERY_PATH); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true);//设置是否允许数据写入 connection.setDoOutput(true);//设置是否允许参数数据输出 connection.setConnectTimeout(5000);//设置链接响应时间 connection.setReadTimeout(10000);//设置参数读取时间 connection.setRequestProperty("Content-type","application/x-www-form-urlencoded"); //提交请求 out=new OutputStreamWriter(connection.getOutputStream(),"UTF-8"); String args=getQueryArgs(ACCOUNT_SID, tamp, phone, timestamp, sig, "JSON"); out.write(args); out.flush(); //读取返回参数 br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String temp=""; while((temp=br.readLine())!=null){ result.append(temp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject json=new JSONObject(result.toString()); String respCode=json.getString("respCode"); String defaultRespCode="00000"; if(defaultRespCode.equals(respCode)){ return rod; }else{ return defaultRespCode; } } //定义一个请求参数拼接方法 public static String getQueryArgs(String accountSid,String smsContent,String to,String timestamp,String sig,String respDataType){ return "accountSid="+accountSid+"&smsContent="+smsContent+"&to="+to+"×tamp="+timestamp+"&sig="+sig+"&respDataType="+respDataType; } //获取时间戳 public static String getTimestamp(){ return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); } //sing签名 public static String getMD5(String sid,String token,String timestamp){ StringBuilder result=new StringBuilder(); String source=sid+token+timestamp; //获取某个类的实例 try { MessageDigest digest=MessageDigest.getInstance("MD5"); //要进行加密的东西 byte[] bytes=digest.digest(source.getBytes()); for(byte b:bytes){ String hex=Integer.toHexString(b&0xff); if(hex.length()==1){ result.append("0"+hex); }else{ result.append(hex); } } } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result.toString(); } //创建验证码 public static String smsCode(){ String random=(int)((Math.random()*9+1)*100000)+""; return random; } }

sunlea LV20
2024年6月24日
libo1212 LV8
2024年6月17日
936684178 LV2
2023年6月27日
annazhang LV29
2023年4月8日
快乐的程序员 LV25
2023年1月21日
二郎滩的大帅 LV3
2022年9月7日
陈王子 LV12
2022年7月18日
微信网友_5916283298041856 LV4
2022年6月7日
yangyue123 LV1
2022年5月17日
zz1230012300 LV11
2022年3月21日

asdxvfvfs
2024年12月3日
暂无贡献等级
black8angel LV4
2024年10月29日
libo1212 LV8
2024年6月17日
sunlea LV20
2024年6月5日
YiRenHun LV11
2023年12月5日
guyue123456
2023年11月23日
暂无贡献等级
jiemomo LV12
2023年10月19日
漫步的海星 LV4
2023年9月21日
zj20020510 LV5
2023年9月18日
936684178 LV2
2023年6月27日