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

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;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值