package cn.timer.api.utils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; 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.regex.Matcher; import java.util.regex.Pattern; /** * 短信服务 * @author ljh * */ public class SMS { /* * webservice */ // private static String serviceURL = "http://sdk.entinfo.cn:8061/webservice.asmx"; // private static String sn = "SDK-WSS-010-08618"; // private static String pwd = "565656"; private static String serviceURL = "http://113.108.68.228:8001/sendSMS.action"; private static String serviceURL2 = "http://47.106.188.166:38202/smsgwhttp/sms/submit"; private static String serviceURL3 = "http://47.111.143.198:8888/V2sms.aspx"; // private static String sn = "SDK-WSS-010-08618"; // private static String pwd = "565656"; /** * md5 * @param sourceStr * @return * @throws UnsupportedEncodingException */ private String getMD5(String sourceStr){ String resultStr = ""; try { byte[] temp = sourceStr.getBytes(); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(temp); // resultStr = new String(md5.digest()); byte[] b = md5.digest(); for (int i = 0; i < b.length; i++) { char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = digit[(b[i] >>> 4) & 0X0F]; ob[1] = digit[b[i] & 0X0F]; resultStr += new String(ob); } return resultStr; } catch (NoSuchAlgorithmException e) { return null; } } /* * mdgetSninfo * * sn,pwd(md5(sn+password)) * */ /*public String mdgetSninfo() { String result = ""; String soapAction = "http://entinfo.cn/mdgetSninfo"; String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; xml += "<soap:Body>"; xml += "<mdgetSninfo xmlns=\"http://entinfo.cn/\">"; xml += "<sn>" + sn + "</sn>"; xml += "<pwd>" + pwd + "</pwd>"; xml += "<mobile>" + "" + "</mobile>"; xml += "<content>" + "" + "</content>"; xml += "<ext>" + "" + "</ext>"; xml += "<stime>" + "" + "</stime>"; xml += "<rrid>" + "" + "</rrid>"; xml += "<msgfmt>" + "" + "</msgfmt>"; xml += "</mdgetSninfo>"; xml += "</soap:Body>"; xml += "</soap:Envelope>"; URL url; try { url = new URL(serviceURL); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(xml.getBytes()); byte[] b = bout.toByteArray(); httpconn.setRequestProperty("Content-Length", String .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "text/xml; charset=gb2312"); httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { Pattern pattern = Pattern.compile("<mdgetSninfoResult>(.*)</mdgetSninfoResult>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } }*/ /* * mdgxsend * * mobile,content,ext,stime,rrid,msgfmt() * */ /*public String mdgxsend(String mobile, String content, String ext, String stime, String rrid, String msgfmt) { String result = ""; String soapAction = "http://entinfo.cn/mdgxsend"; String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; xml += "<soap:Body>"; xml += "<mdgxsend xmlns=\"http://entinfo.cn/\">"; xml += "<sn>" + sn + "</sn>"; xml += "<pwd>" + this.getMD5(sn + pwd) + "</pwd>"; xml += "<mobile>" + mobile + "</mobile>"; xml += "<content>" + content + "</content>"; xml += "<ext>" + ext + "</ext>"; xml += "<stime>" + stime + "</stime>"; xml += "<rrid>" + rrid + "</rrid>"; xml += "<msgfmt>" + msgfmt + "</msgfmt>"; xml += "</mdgxsend>"; xml += "</soap:Body>"; xml += "</soap:Envelope>"; System.out.println(xml); URL url; try { url = new URL(serviceURL); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(xml.getBytes("utf-8")); byte[] b = bout.toByteArray(); httpconn.setRequestProperty("Content-Length", String .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "text/xml; charset=gb2312"); httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { Pattern pattern = Pattern.compile("<mdgxsendResult>(.*)</mdgxsendResult>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } }*/ /** * ems发送平台 * @param mobile * @param content * @param ext * @param stime * @param rrid * @param msgfmt * @return */ public String mdgxsend(String mobile, String content, String ext, String stime, String rrid, String msgfmt) { String result = ""; String postParams = null; try { postParams = "enterpriseID=" + URLEncoder.encode("17557", "UTF-8"); postParams +="&loginName="+URLEncoder.encode("admin", "UTF-8"); postParams +="&password="+URLEncoder.encode(getMD5("shtj322").toLowerCase(), "UTF-8"); postParams +="&content="+content; postParams +="&mobiles="+URLEncoder.encode(mobile, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(postParams); URL url; try { url = new URL(serviceURL); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(postParams.getBytes("utf-8")); byte[] b = bout.toByteArray(); // httpconn.setRequestProperty("Content-Length", String // .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { Pattern pattern = Pattern.compile("<Result>(.*)</Result>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } } /* * mdsmssend * mobile,content,ext,stime,rrid,msgfmt() */ /*public String mdsmssend(String mobile, String content, String ext, String stime, String rrid,String msgfmt) { String result = ""; String soapAction = "http://entinfo.cn/mdsmssend"; String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; xml += "<soap:Body>"; xml += "<mdsmssend xmlns=\"http://entinfo.cn/\">"; xml += "<sn>" + sn + "</sn>"; xml += "<pwd>" + pwd + "</pwd>"; xml += "<mobile>" + mobile + "</mobile>"; xml += "<content>" + content + "</content>"; xml += "<ext>" + ext + "</ext>"; xml += "<stime>" + stime + "</stime>"; xml += "<rrid>" + rrid + "</rrid>"; xml += "<msgfmt>" + msgfmt + "</msgfmt>"; xml += "</mdsmssend>"; xml += "</soap:Body>"; xml += "</soap:Envelope>"; URL url; try { url = new URL(serviceURL); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(xml.getBytes("utf-8")); byte[] b = bout.toByteArray(); httpconn.setRequestProperty("Content-Length", String .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "text/xml; charset=gb2312"); httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } }*/ /** * ems发送平台 * @param mobile * @param content * @return */ public String mdgxsend2(String mobile, String content) { String result = ""; String postParams = null; SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String timestamp = df.format(new Date()); try { postParams = "spid=" + URLEncoder.encode("321526", "UTF-8"); postParams +="&password="+URLEncoder.encode(getMD5("321526" + timestamp + "1234aBcd"), "UTF-8"); postParams +="&ac="+URLEncoder.encode("1069321526", "UTF-8"); postParams +="×tamp="+URLEncoder.encode(timestamp, "UTF-8"); postParams +="&mobiles="+URLEncoder.encode(mobile, "UTF-8"); postParams +="&content="+content; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(postParams); URL url; try { url = new URL(serviceURL2); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(postParams.getBytes("utf-8")); byte[] b = bout.toByteArray(); // httpconn.setRequestProperty("Content-Length", String // .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { System.out.println(inputLine); Pattern pattern = Pattern.compile("<Result>(.*)</Result>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } } /** * ems发送平台 * @param mobile * @param content * @return */ public String mdgxsend3(String mobile, String content) { String userid = "18"; String username = "youling"; String password = "abc12345"; String result = ""; String postParams = null; SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String timestamp = df.format(new Date()); postParams = "action=send"; postParams += "&userid=" + userid; postParams += "×tamp=" + timestamp; postParams += "&sign=" + getMD5(username + password + timestamp); postParams += "&mobile=" + mobile; postParams += "&content=" + content; postParams += "&sendTime="; postParams += "&extno="; System.out.println(postParams); URL url; try { url = new URL(serviceURL3); URLConnection connection = url.openConnection(); HttpURLConnection httpconn = (HttpURLConnection) connection; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(postParams.getBytes("utf-8")); byte[] b = bout.toByteArray(); // httpconn.setRequestProperty("Content-Length", String // .valueOf(b.length)); httpconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // httpconn.setRequestProperty("SOAPAction", soapAction); httpconn.setRequestMethod("POST"); httpconn.setDoInput(true); httpconn.setDoOutput(true); OutputStream out = httpconn.getOutputStream(); out.write(b); out.close(); InputStreamReader isr = new InputStreamReader(httpconn .getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while (null != (inputLine = in.readLine())) { System.out.println(inputLine); Pattern pattern = Pattern.compile("<message>(.*)</message>"); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) { result = matcher.group(1); } } return result; } catch (Exception e) { e.printStackTrace(); return ""; } } public static String sendSms(String mobile,String content) throws UnsupportedEncodingException{ String result_gxmt = new SMS().mdgxsend3(mobile, URLEncoder.encode(content+"【8小时人事管家】", "utf-8")); //String result_gxmt = new SMS().mdgxsend(mobile, URLEncoder.encode(content+"【8小时人事管家】", "utf-8"), "", "", "", ""); //String result_gxmt = new SMS().mdgxsend2(mobile, URLEncoder.encode(content+"【8小时人事管家】", "utf-8")); return result_gxmt; } public static String createCode(){ String num = ""; for (int i = 0; i < 6; i++) { num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1)); } return num; } // public static void main(String []args) throws UnsupportedEncodingException{ String code = createCode(); System.out.println(code); sendSms("13129310893","您的短信验证码是"+code); } }