目前,短信验证码已经被广泛应用在各大网站、app应用中的用户注册、密码找回等场景,那么怎样实现发送短信验证码?以java开发语言为例,为大家分享想调用短信接口的实现代码,希望对大家有所帮助!!话不多说,直接上代码!!互亿无线为例:两种短信形式,一种验证码短信,一种自定义短信
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* 手机验证码
* @author Administrator
*
*/
public class PhoneCodeUtils {
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
public static Map<String,String> sendCode(String phoneNum){
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
Map<String,String> msgMap=new HashMap<String,String>();
//client.getParams().setContentCharset("GBK");
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
int mobile_code = (int)((Math.random()*9+1)*100000);
String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");
NameValuePair[] data = {//提交短信
new NameValuePair("account", "用户名"),
new NameValuePair("password", "密码"), //密码可以使用明文密码或使用32位MD5加密
new NameValuePair("mobile", phoneNum),
new NameValuePair("content", content),
};
method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
//System.out.println(SubmitResult);
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");
msgMap.put("code", code);
msgMap.put("msg", msg);
msgMap.put("smsid", smsid);
msgMap.put("phoneCode", String.valueOf(mobile_code));
System.out.println(code);
System.out.println(msg);
System.out.println(smsid);
return msgMap;
} catch (HttpException e) {
if(e.getMessage().toLowerCase().contains("java.net.UnknownHostException".toLowerCase())){
msgMap.put("code", "2");
msgMap.put("msg", "非法的请求域名");
return msgMap;
}
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 发送自定义信息
* @param phoneNum
* @param message
* @return
*/
public static Map<String,String> sendMsg(String phoneNum, String message){
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
//client.getParams().setContentCharset("GBK");
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
String content = new String(message);
NameValuePair[] data = {//提交短信
new NameValuePair("account", "用户名"),
new NameValuePair("password", "密码"), //密码可以使用明文密码或使用32位MD5加密
new NameValuePair("mobile", phoneNum),
new NameValuePair("content", content),
};
method.setRequestBody(data);
Map<String,String> msgMap=new HashMap<String,String>();
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
//System.out.println(SubmitResult);
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
String code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");
msgMap.put("code", code);
msgMap.put("msg", msg);
msgMap.put("smsid", smsid);
return msgMap;
} catch (HttpException e) {
if(e.getMessage().toLowerCase().contains("java.net.UnknownHostException".toLowerCase())){
msgMap.put("code", "2");
msgMap.put("msg", "非法的请求域名");
return msgMap;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}