短信验证码接口JAVA示例模板
林嘉雯
2022-03-05 05:04:00
共 1 个回答
王晓雅
2022-03-09 04:06:58
短信发送代码示例//接口地址String url = "http://IP/端口";//下发时间String mttime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());Map param = new HashMap();param.put("name", "用户帐号");param.put("pwd", Tools.MD5("用户密码"+mttime));param.put("content", URLEncoder.encode("【阅信短信验证码】验证码888888,打死也不能告诉别人哦。", "UTF-8"));param.put("phone", "13400000000");param.put("subid", "");param.put("mttime", mttime);HttpTool.sendPost(url, param);
POST提交方法public static String sendPost(String url, Map params) {Log.i("POST提交:[url="+url+"]"+params.toString());URL u = null;HttpURLConnection con = null;// 构建请求参数StringBuffer sb = new StringBuffer();if (params != null) {for (Entry e : params.entrySet()) {sb.append(e.getKey()).append("=").append(e.getValue()).append("&");}sb.substring(0, sb.length() - 1);}// 尝试发送请求try {u = new URL(url);con = (HttpURLConnection) u.openConnection();con.setRequestMethod("POST");con.setConnectTimeout(6000);con.setDoOutput(true);con.setDoInput(true);con.setUseCaches(false);con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");osw.write(sb.toString());osw.flush();osw.close();} catch (Exception e) {Log.e(e);} finally {if (con != null) {con.disconnect();}}// 读取返回内容StringBuffer buffer = new StringBuffer();try {BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));String temp;while ((temp = br.readLine()) != null) {buffer.append(temp).append("\n");}} catch (Exception e) {Log.e(e);}Log.i("POST响应:"+buffer.toString());return buffer.toString();}
MD5加密方法public static String MD5(String str){MessageDigest md5 = null;      try{          md5 = MessageDigest.getInstance("MD5");      }catch (Exception e){          Log.i(e.getMessage());        return "";      }      char[] charArray = str.toCharArray();      byte[] byteArray = new byte[charArray.length];      for (int i = 0; i 
阅读原文