中国网建 java_java 中国网建实现发送短信验证码

现在中国网建上注册一个自己的账户,

然后里面有代码案例,也有相应的下载jar包的地址

package com.direct.note;

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 Message {

public static void main(String[] args) {

try {

new Message().sendMessage();

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public void sendMessage() throws HttpException, 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");

int count = (int)(Math.random()*10000)+1;

System.out.println(count);

String message = "验证码:"+count;

NameValuePair[] data = {

new NameValuePair("Uid", "theStar"),

new NameValuePair("Key", "你的key"),//73d1e45fd5f0fa2b9a87

new NameValuePair("smsMob", "发送的电话号码"),

new NameValuePair("smsText", message)

};

post.setRequestBody(data);

client.executeMethod(post);

Header[] headers = post.getRequestHeaders();

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现手机验证码平台可以使用第三方平台接口来发送验证码。以下是实现步骤: 1. 注册第三方平台账号,并获取API接口息。 2. 在Java项目中引入第三方平台的SDK(软件开发工具包)。 3. 编写Java代码,调用SDK提供的接口,传递内容和手机号码,发送验证码。 以下是一个简单的Java发送验证码的示例代码: ```java // 引入第三方平台SDK import com.aliyun.dysmsapi20170525.Client; import com.aliyun.dysmsapi20170525.models.*; import com.aliyun.teaopenapi.models.Config; public class SmsUtil { // 设置API接口息 private static final String accessKeyId = "yourAccessKeyId"; private static final String accessKeySecret = "yourAccessKeySecret"; private static final String signName = "yourSignName"; private static final String templateCode = "yourTemplateCode"; public static void sendSms(String mobile, String code) throws Exception { // 创配置对象 Config config = new Config() .setAccessKeyId(accessKeyId) .setAccessKeySecret(accessKeySecret); // 创Client对象 Client client = new Client(config); // 创发送请求对象 SendSmsRequest sendSmsRequest = new SendSmsRequest() .setPhoneNumbers(mobile) .setSignName(signName) .setTemplateCode(templateCode) .setTemplateParam("{\"code\":\"" + code + "\"}"); // 发送 SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest); System.out.println(sendSmsResponse.getBody()); } } ``` 在上述代码中,我们使用了阿里云平台提供的SDK来实现发送验证码的功能。需要事先在阿里云平台注册账号,并获取相应的API接口息。然后,我们通过创Config对象来设置API接口息,并使用Client对象来发送。在发送时,需要设置手机号码、签名、模板ID和模板参数等息。最后,我们将发送的结果输出到控制台。 以上就是基于Java实现手机验证码平台的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值