Java使用中国网建发送短信验证码

一.注册,登录以及注意事项。

1.1 中国网建网址:http://www.smschinese.cn

1.2 注册时用户名,以及姓名都可以随便写,邮箱只会验证存在与否,不会发验证码验证,手机号填自己的(接收自己的初始密码)。

1.3 登录到用户首页,有快捷菜单,发送短信;发送彩信;Excel短信,先点击发送短信,一定要先设置签名(否则无法发送短信)。

1.4 在旁边的菜单中有修改短信密钥,记录下短信密钥。

二.Java示例。

2.1 接口地址

GBK编码发送接口地址
http://gbk.api.smschinese.cn/?Uid=本站用户名&Key=短信密钥&smsMob=手机号码&smsText=短信内容 
UTF-8编码发送接口地址:
http://utf8.api.smschinese.cn/?Uid=本站用户名&Key=短信密钥&smsMob=手机号码&smsText=短信内容
获取短信数量接口地址(UTF8):
http://www.smschinese.cn/web_api/SMS/?Action=SMS_Num&Uid=本站用户名&Key=短信密钥
获取短信数量接口地址(GBK):

http://www.smschinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=本站用户名&Key=短信密钥

2.2 短信发送后返回值以及意义

-1(没有该账户);-2(短信密钥错误);-3(短信数量不足);-4(手机号不正确);-6(IP被限制);-14(短信内容出现非法字符);-21(MD5接口密钥加密不正确);-41(手机号为空);-42(短信内容为空);-51(短信签名格式不正确
接口签名格式为:【签名内容】)。

2.3 Java代码

import java.io.IOException;
import org.apache.commons.httpclient.Header;
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;

public class SendMsg {

private static final String SERVER_URL = "http://utf8.api.smschinese.cn";
private static final String USERNAME = "你的用户名";
private static final String KEY = "短信密钥";

public static void main(String[] args) throws Exception {
String phone = "被发送的短信的手机号";
String smsText = "短信发送的内容";
String result = SendMessage(phone, smsText);
int allResult = Integer.valueOf(result).intValue();
if (allResult > 0) {
System.out.println("发送成功");
} else {
System.out.println("发送失败");
}
}
public static String SendMessage(String phone, String smsText) throws HttpException, IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(SERVER_URL);
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
NameValuePair[] data = { new NameValuePair("Uid", USERNAME), new NameValuePair("Key", KEY),
new NameValuePair("smsMob", phone), new NameValuePair("smsText", smsText) };
post.setRequestBody(data);
client.executeMethod(post);
// 请求头信息
Header[] headers = post.getResponseHeaders();
// 输出请求头信息
/*
* for (Header h : headers) { System.out.println(h.toString()); }
*/
// http状态码
int statusCode = post.getStatusCode();
System.out.println("statusCode:" + statusCode);
String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
System.out.println(result); // 打印返回消息状态
post.releaseConnection();
return result;
}
}

若是哪里有理解错误的或写错的地方,望各位读者评论或者私信指正,不胜感激。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
发送短信验证码,你需要使用短信服务提供商的 API。以下是一个使用阿里云短信服务的示例代码: ```java import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sms.model.v20170605.SendSmsRequest; import com.aliyuncs.sms.model.v20170605.SendSmsResponse; public class SmsSender { private static final String ACCESS_KEY_ID = "your_access_key_id"; private static final String ACCESS_KEY_SECRET = "your_access_key_secret"; private static final String SIGN_NAME = "your_sign_name"; private static final String TEMPLATE_CODE = "your_template_code"; public static void sendSms(String phone) throws ClientException { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com"); IAcsClient client = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setPhoneNumbers(phone); request.setSignName(SIGN_NAME); request.setTemplateCode(TEMPLATE_CODE); String code = String.format("%04d", new Random().nextInt(10000)); request.setTemplateParam("{\"code\":\"" + code + "\"}"); SendSmsResponse response = client.getAcsResponse(request); System.out.println("短信发送结果:" + response.getCode()); } } ``` 在上面的代码中,你需要替换以下常量: - `your_access_key_id`:你的阿里云 Access Key ID - `your_access_key_secret`:你的阿里云 Access Key Secret - `your_sign_name`:你在阿里云短信服务中设置的签名名称 - `your_template_code`:你在阿里云短信服务中设置的短信模板编号 使用时,你只需要调用 `sendSms` 方法并传入要发送的手机号码即可。该方法会生成一个 4 位的随机验证码并发送到指定的手机号码上。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值