簡訊驗證碼demo
package cn.appInterface.util;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import cn.common.util.RandomStringGenerator;
public class Client {
/*
* webservice伺服器定義
*/
private String serviceURL = " http:sdk*******";//
private String sn = " sdk*****";// 賬號
private String password = " ******";// 密碼
private String pwd = "";
public Client()
throws UnsupportedEncodingException {
//密碼為md5(sn+password)
this.pwd = this.getMD5(sn + password);
}
/*
* 建構函式175259202
*/
public Client(String sn, String password)
throws UnsupportedEncodingException {
this.sn = sn;
this.password = password;
//密碼為md5(sn+password)
this.pwd = this.getMD5(sn + password);
}
/*
* 方法名稱:getMD5
* 功 能:字串MD5加密
* 參 數:待轉換字串
* 返 回 值:加密之後字串
*/
public static String getMD5(String sourceStr) throws UnsupportedEncodingException {
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) {
e.printStackTrace();
return null;
}
}
/*
* 方法名稱:mdgetSninfo
* 功 能:獲取資訊
* 參 數:sn,pwd(軟體序列號,加密密碼md5(sn+password))
*
*/
public String mdgetSninfo() throws IOException {
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;
ByteArrayOutputStream bout = null;
OutputStream out = null;
InputStreamReader isr = null;
BufferedReader in = null;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
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);
out = httpconn.getOutputStream();
out.write(b);
isr = new InputStreamReader(httpconn
.getInputStream());
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 "";
}finally {
if (bout!=null) {
bout.close();
}
if(out!=null){
out.close();
}
if(isr!=null){
isr.close();
}
if(in!=null){
in.close();
}
}
}
/*
* 方法名稱:mdgxsend
* 功 能:傳送個性簡訊
* 參 數:mobile,content,ext,stime,rrid,msgfmt(手機號,內容,擴充套件碼,定時時間,唯一標識,內容編碼)
* 返 回 值:唯一標識,如果不填寫rrid將返回系統生成的
*/
public String mdgxsend(String mobile, String content, String ext, String stime,
String rrid, String msgfmt) throws IOException {
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>" + 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>";
ByteArrayOutputStream bout = null;
OutputStream out = null;
InputStreamReader isr = null;
BufferedReader in = null;
URL url;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
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);
out = httpconn.getOutputStream();
out.write(b);
out.close();
isr = new InputStreamReader(httpconn
.getInputStream());
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 "";
}finally {
if (bout!=null) {
bout.close();
}
if(out!=null){
out.close();
}
if(isr!=null){
isr.close();
}
if(in!=null){
in.close();
}
}
}
/*
* 方法名稱:mdsmssend
* 功 能:傳送簡訊
* 參 數:mobile,content,ext,stime,rrid,msgfmt(手機號,內容,擴充套件碼,定時時間,唯一標識,內容編碼)
* 返 回 值:唯一標識,如果不填寫rrid將返回系統生成的
*/
public String mdsmssend(String mobile, String content, String ext, String stime,
String rrid,String msgfmt) throws IOException {
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>";
ByteArrayOutputStream bout = null;
OutputStream out = null;
InputStreamReader isr = null;
BufferedReader in = null;
URL url;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
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);
out = httpconn.getOutputStream();
out.write(b);
out.close();
isr = new InputStreamReader(httpconn
.getInputStream());
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 "";
}finally {
if (bout!=null) {
bout.close();
}
if(out!=null){
out.close();
}
if(isr!=null){
isr.close();
}
if(in!=null){
in.close();
}
}
}
public boolean sendSdkJx(String phone,String sdkCode){
try {
//String sdkCode=RandomStringGenerator.getRandomStringByLengthNUM(6);
String content = java.net.URLEncoder.encode("【**體彩】您的驗證碼是:"+sdkCode+" 登入**體彩微信公眾號,免費參加世界盃小組賽模擬競猜,贏競彩體驗劵。請閱讀活動規則", "utf-8");
String msg=mdsmssend(phone, content, "", "", "", "");
System.out.println("shijiebei:"+msg);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean sendSdk(HttpServletRequest request,String phone){
try {
String sdkCode=RandomStringGenerator.getRandomStringByLengthNUM(6);
String content = java.net.URLEncoder.encode("[****]尊敬的中國體彩網使用者,您的簡訊隨機碼是:"+sdkCode+",請您在10分鐘內進行認證", "utf-8");
mdsmssend(phone, content, "", "", "", "");
request.getSession().setAttribute("sdkCdoe", sdkCode+"-"+(new Date()).getTime());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean sendSdk(String phone){
try {
String sdkCode=RandomStringGenerator.getRandomStringByLengthNUM(6);
String content = java.net.URLEncoder.encode("[****]尊敬的中國體彩網使用者", "utf-8");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean sendSdk(String phone,String code){
try {
String content = java.net.URLEncoder.encode("【****】尊敬的使用者您好,您的驗證碼是:"+code, "utf-8");
String str=mdsmssend(phone, content, "", "", "", "");
System.out.println("返回:"+str);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) throws UnsupportedEncodingException {
Client c=new Client();
c.sendSdk("18211034428", "111111");
}
}