简介、
鄙人用的是Java,其他语言也可以用如:c语言、c++、python、java、php等,具体如下
一、短信平台
互亿无线 网址:https://www.ihuyi.com
二、步骤
1.注册一个账号
新用户会送几条免费短信,具体几条我也忘记了。
注意:注册号码后会有平台工作人员打电话给你询问你注册这个号干嘛的,其实就是拉你做客户,不用紧张。
----登录进去后在首页可以看到自己的电话号码,和短信余额,然后点击验证码通知短信
2.下载接口文档
下载好的文档是个压缩包,解压就可以了,是这样的: 选择你需要的语言就行啦
3.互亿在Java中的短信验证
我们找到java所在的文件夹 点到这个位置 lib下的是jar包依赖 src下的是实现主要代码
本人是pom文件注入依赖的
<!-- 短信验证jar包 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0-rc4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
导入jar包后就把src下的代码放入项目中
工具类:
package com.zking.ssm.test.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];
}
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;
}
}
实现功能的类有两个只是编码方式不一样是sendsms和sendsmsutf-8, 这里要用到你账户的APIID和APIKEY还有手机号了 account:对应的是APIID password:对应的是APIKEY mobile:手机号
sendsms类:
package com.zking.ssm.test;
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 com.zking.ssm.test.util.StringUtil;
public class sendsms {
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
public static void main(String [] args) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
client.getParams().setContentCharset("GBK");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");
int mobile_code = (int)((Math.random()*9+1)*100000);
String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");
NameValuePair[] data = {
new NameValuePair("account", "C84099173"),
new NameValuePair("password", "4d6bce3d366d986fa59560484906dd94"),
new NameValuePair("mobile", "191******72"),
new NameValuePair("content", content),
};
method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
sendsmsutf8:
package com.zking.ssm.test;
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 com.zking.ssm.test.util.StringUtil;
public class sendsmsutf8 {
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
public static void main(String [] args) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
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", "C84099173"),
new NameValuePair("password", "4d6bce3d366d986fa59560484906dd94"),
new NameValuePair("mobile", "191******72"),
new NameValuePair("content", content),
};
method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
配置好后,运行就能收到短信啦 我收到的短信截图:
总结
over,如有问题,可以在评论区留言哦。
|