作者:萝莉吴文娟 | 来源:互联网 | 2023-08-11 14:02
一:打开APISpace官网,登录,搜索短信验证,点击立即购买,新用户会送十条短信https:www.apispace.com 二:打开我的Api,找到刚刚购买
一:打开APISpace官网,登录,搜索短信验证,点击立即购买,新用户会送十条短信
https://www.apispace.com/
二:打开我的Api,找到刚刚购买的短信流量包,复制提供的代码
三:打开IDEA,导入xml文件,编写控制类
com.squareup.okhttp3
okhttp
3.14.9
cn.smallbun.screw
screw-core
1.0.5
//这里呢我把一些相关信息绑定在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();
}