java手机验证码接口
时间: 2023-10-20 18:36:23 浏览: 60
Java 手机验证码接口可以通过第三方短信服务商提供的 API 来实现。以下是一个简单的示例代码:
```
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SmsUtils {
private static final String API_URL = "http://sms-api.luosimao.com/v1/send.json";
private static final String API_KEY = "your_api_key";
public static boolean sendSms(String mobile, String message) {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(API_URL);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String sign = DigestUtils.md5Hex("api_key" + API_KEY + "message" + message + "mobile" + mobile + "timestamp" + timestamp);
Map<String, Object> params = new HashMap<>();
params.put("mobile", mobile);
params.put("message", message);
params.put("timestamp", timestamp);
params.put("sign", sign);
try {
post.setEntity(new UrlEncodedFormEntity(buildNameValuePairList(params), "utf-8"));
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonResult = JSONObject.parseObject(result);
if (jsonResult.getIntValue("error") == 0) {
return true;
} else {
System.out.println(jsonResult.getString("msg"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static List<NameValuePair> buildNameValuePairList(Map<String, Object> params) {
List<NameValuePair> nameValuePairList = new ArrayList<>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
nameValuePairList.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
return nameValuePairList;
}
}
```
在使用时,需要将 `API_KEY` 替换成你自己的 API Key,然后调用 `sendSms` 方法即可发送短信验证码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)