没有阿里云短信账号的同学可以选择httpClient方式发送短信
一、注册账号
去中国网建 注册一个账号,或者去其他短信网站注册。注册成功后会赠送5条短信,在修改用户信息里填写你的短信签名,发送短信时格式如下:
【短信签名】 短信内容

点击修改短信秘钥,可查看你的短信秘钥,短信接口中需要填写此信息,勿泄露,如图:

二、下载jar包
点击短信API接口,跳转至接口介绍页面,有各种语言发送短信的案例

在 3.JAVA调用案例下面下载jar包

三、java发送短信代码
public static void sendMsg(String phone,String text) throws IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
NameValuePair[] data ={ new NameValuePair("Uid", "你的登录名"),new NameValuePair("Key", "你的短信秘钥"),new NameValuePair("smsMob",phone),new NameValuePair("smsText",text)};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:"+statusCode);
for(Header h : headers)
{
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println("打印返回消息状态:"+result); //打印返回消息状态
post.releaseConnection();
}
main函数发送短信测试
public static void main(String[] args) {
try {
sendMsg("176xxxxxxxx","登录验证码为:"+800632);
} catch (IOException e) {
e.printStackTrace();
}
}
关注我的微信公众号获取更多资源

3260

被折叠的 条评论
为什么被折叠?



