一:打开APISpace官网,登录,搜索短信验证,点击立即购买,新用户会送十条短信
二:打开我的Api,找到刚刚购买的短信流量包,复制提供的代码
三:打开IDEA,导入xml文件,编写控制类
<!-- 短信验证--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.14.9</version> </dependency> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency>
//这里呢我把一些相关信息绑定在yaml中,防止泄露
#短信验证 sms: url_code: ? value: ?
import io.swagger.annotations.ApiParam; import okhttp3.*; import okhttp3.RequestBody; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.io.IOException; @RestController @RequestMapping("/SMS") public class SmsApiController { @Value("${sms.url_code}") String url_code; @Value("${sms.value}") String value; @PostMapping("/Api/{phone}/{code}") public void SMSApi(@ApiParam(value = "手机号码", required = false) @PathVariable String phone, @ApiParam(value = "验证码", required = false) @PathVariable int code ) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\"msg\":\"【】:您的验证码是{$var}\",\"params\":" + "\"" + "" + phone + "," + code + "\",\"sendtime\":\"\",\"report\":\"false\",\"extend\":\"\",\"uid\":\"\"}"); Request request = new Request.Builder() .url(url_code) .method("POST", body) .addHeader("X-APISpace-Token", value) .addHeader("Authorization-Type", "apikey") .addHeader("Content-Type", "application/json") .build(); Response response = client.newCall(request).execute(); }
标签:code,短信,验证,url,三分钟,value,Value,import From: https://www.cnblogs.com/zsbb/p/16743725.html