您的位置:首页 > 其它

借助短信平台实现手机注册页面的验证吗发送检验功能

2016-01-25 15:44 1061 查看
package com.other.helper;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URLDecoder;

import java.util.Random;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.URI;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import com.bcloud.msg.http.HttpSender;

/** 短信验证码发送 */

public class MsgCodeHelper {

private static String contentCharset = "UTF-8";
private static String contentType = "ContentType";
private static String contentTypeStr = "application/x-www-form-urlencoded;charset=UTF-8";
// 1aqyPlag59hJBPYO

public MsgCodeHelper() {

}

/*public static boolean sendMsgCode(String phoneNo, String content) {

System.out.println("content="+content);
boolean flag = false;
HttpClient client = new HttpClient(); 
PostMethod method = new PostMethod(SystemConfigHelp.getSystemConfig().getSmsUrl());
client.getParams().setContentCharset(contentCharset);
method.setRequestHeader(contentType, contentTypeStr); 
NameValuePair[] data = { // 提交短信
new NameValuePair("account", SystemConfigHelp.getSystemConfig().getSmsAccount()), new NameValuePair("password", SystemConfigHelp.getSystemConfig().getSmsPassword()), // 密码可以使用明文密码或使用32位MD5加密
// new NameValuePair("password",
// util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", phoneNo), new NameValuePair("content", content),};
method.setRequestBody(data);

try { 
client.executeMethod(method);  
String SubmitResult = method.getResponseBodyAsString();
System.out.println("SubmitResult="+SubmitResult);
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");  
System.out.println("code="+code);
if (code.equals("2")) {
flag = true; 
}
} catch (HttpException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
} catch (DocumentException e) {

e.printStackTrace();
}
return flag;
}

*/

/**

* @param phoneNo
* @param content
* @return
* @throws Exception
*/
public static Boolean sendMsgCode(String phoneNo, String content)throws Exception {
content="亲爱的用户,您的验证码是123456,5分钟内有效。";
boolean flag = false;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod();
try {
URI base = new URI(SystemConfigHelp.getSystemConfig().getSmsUrl(), false);
method.setURI(new URI(base, "HttpBatchSendSM", false));
method.setQueryString(new NameValuePair[] { 
new NameValuePair("account", SystemConfigHelp.getSystemConfig().getSmsAccount()),
new NameValuePair("pswd", SystemConfigHelp.getSystemConfig().getSmsPassword()), 
new NameValuePair("mobile", phoneNo),
new NameValuePair("needstatus", String.valueOf(true)), 
new NameValuePair("msg", content),
new NameValuePair("product", null),
new NameValuePair("extno", null), 
});
int result = client.executeMethod(method);
if (result == HttpStatus.SC_OK) {
InputStream in = method.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String[] str=URLDecoder.decode(baos.toString(), "UTF-8").split(",");
String[] s=str[1].split("\\r?\\n");
if(s[0].equals("0"))
{
flag=true;
}
return flag;

else {
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
}
} finally {
method.releaseConnection();
}
}
/** 生成随机 6位验证码 codeSize为验证码长度 */
public static String RandomCode(int codeSize) {
String code = "";
Random random = new Random();
for (int i = 0; i < codeSize; i++) {
code += random.nextInt(10);
}
return code;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: