利用第三方接口发送短信(工具:Eclipse)
Posted 柴鹏举
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用第三方接口发送短信(工具:Eclipse)相关的知识,希望对你有一定的参考价值。
注:此处只是粘贴的代码;有想要jar包或者有哪块本人讲的不清楚的请留言!(使用这个的前提是你得在这个第三方平台注册一个号:www.ihuyi.com
)
//讲的不好请指教!
import java.io.IOException;
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;
import util.StringUtil;
public class sendsms {
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
//设置形参到时候直接调用 形参手机号
public void SendMessage(String tel){
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
client.getParams().setContentCharset("GBK");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");
//Math.random() 范围(0,1)*9(0,9) 【包括0,不包括1 注意 】;所以 *100000 (0,10000) 不包括右端得数
int mobile_code = (int)((Math.random()*9+1)*100000);
System.out.println("code: "+mobile_code);
String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");
NameValuePair[] data = {//提交短信
new NameValuePair("account", "APIID"), //查看用户名请登录用户中心->验证码、通知短信->帐户及签名设置->APIID
new NameValuePair("password", "APIKEY"), //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile",tel ),
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");
System.out.println(code);
System.out.println(msg);
System.out.println(smsid);
if("2".equals(code)){
System.out.println("短信提交成功");
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] args) {
String tel="18434391755";
new sendsms().SendMessage(tel);
}
}
==================================================
(工具util)
import java.security.MessageDigest;
public class StringUtil {
public static String str;
public static final String EMPTY_STRING = "";
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
/**
* 转换字节数组为16进制字串
* @param b 字节数组
* @return 16进制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
} catch (Exception ex) {
}
return resultString;
}
}
以上是关于利用第三方接口发送短信(工具:Eclipse)的主要内容,如果未能解决你的问题,请参考以下文章
个人项目需要发送验证码?无法对接运营商,试试第三方工具阿里云吧