栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 通过SMS短信平台实现发短信功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 通过SMS短信平台实现发短信功能

短信工具类

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class SMSSender {
	private static SMSSender ourInstance = new SMSSender();

	public static SMSSender getInstance() {
		return ourInstance;
	}

	private Gson gson = new Gson();

	private static final String prefix = "【平台APP】";
	private static final Logger logger = LogManager.getLogger(SMSSender.class);
          //发送短信的地址
	private static final String url = "http://IP地址:端口号/sms.aspx?action=send&userid=短信用户id&account=账号&password=密码&mobile=%s&content=%s";


	public boolean sendValidation(String mobile, String content) {
		if (!mobile.matches("1\d{10}")) {
			logger.error("短信发送失败!不合法的手机号:" + mobile);
			return false;
		}
		try {
			return send(mobile, URLEncoder.encode("【平台APP】" + content, "utf-8"));

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
	}

	private boolean send(String mobile, String content) {
		try {
			Response bean = Request.Post(String.format(url, new Object[]{mobile, content})).addHeader("Accept", "text/xml").addHeader("Content-Type", "text/xml;charset=utf-8").execute();
			String result = bean.returnContent().asString();
			return result.contains("Success");
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}


		class Result {
			private int code;
			private int fee;
			private String mobile;
			private String msg;

			Result() {
			}

			public int getCode() {
				return code;
			}

			public void setCode(int code) {
				this.code = code;
			}

			public int getFee() {
				return fee;
			}

			public void setFee(int fee) {
				this.fee = fee;
			}

			public String getMobile() {
				return mobile;
			}

			public void setMobile(String mobile) {
				this.mobile = mobile;
			}

			public String getMsg() {
				return msg;
			}


			public void setMsg(String msg) {
				this.msg = msg;
			}
		}

		class Wrapper {
			private List data;
			@SerializedName("total_fee")
			private int total;

			Wrapper() {
			}

			public List getData() {
				return data;
			}

			public void setData(List data) {
				this.data = data;
			}

			public int getTotal() {
				return total;
			}

			public void setTotal(int total) {
				this.total = total;
			}
		}
	}
}

使用方法:

SMSSender.getInstance().sendValidation("电话号码", "短信内容");

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号