目前,短信驗證碼已經被廣泛應用在各大網站、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;
}
}