Browse Source

--add sendMarketMessage();add sendMarket();

zzy.zhiyuan.foxmail 8 years ago
parent
commit
2edb27808b
1 changed files with 52 additions and 1 deletions
  1. 52 1
      src/main/java/com/ekexiu/portal/mobile/MobilePhoneService.java

+ 52 - 1
src/main/java/com/ekexiu/portal/mobile/MobilePhoneService.java

@ -16,6 +16,7 @@ import org.jfw.apt.annotation.Bean;
16 16
import org.jfw.util.ConstData;
17 17
import org.jfw.util.DateUtil;
18 18
import org.jfw.util.StringUtil;
19
import org.jfw.util.exception.JfwBaseException;
19 20
import org.jfw.util.io.IoUtil;
20 21
import org.jfw.util.json.JsonService;
21 22
@ -26,6 +27,7 @@ public class MobilePhoneService {
26 27
	private String accountSid = "a9e818c5ffb547c6a1db15ce45381ab0";
27 28
	private String authToken = "d34f7d9bccd74d67abe8a62263a73fda";
28 29
	private String restUrl = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
30
	private String marketUrl = "https://api.miaodiyun.com/20150822/affMarkSMS/sendSMS";
29 31
	private int sendTimeout = 10 * 1000;
30 32
	private int readTimeout = 10 * 1000;
31 33
@ -53,6 +55,14 @@ public class MobilePhoneService {
53 55
		this.restUrl = restUrl;
54 56
	}
55 57
58
	public String getMarketUrl() {
59
		return marketUrl;
60
	}
61
62
	public void setMarketUrl(String marketUrl) {
63
		this.marketUrl = marketUrl;
64
	}
65
56 66
	public String getAccountSid() {
57 67
		return accountSid;
58 68
	}
@ -88,6 +98,26 @@ public class MobilePhoneService {
88 98
		InputStream inStream = conn.getInputStream();
89 99
		return IoUtil.readStream(inStream, true);
90 100
	}
101
	
102
	private byte[] sendMarket(String content) throws IOException {
103
		URL url = new URL(this.marketUrl);
104
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
105
		conn.setRequestMethod("POST");// 提交模式
106
		conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
107
		conn.setConnectTimeout(this.sendTimeout);// 连接超时 单位毫秒
108
		conn.setReadTimeout(this.readTimeout);// 读取超时 单位毫秒
109
		conn.setDoOutput(true);// 是否输入参数
110
		byte[] bypes = content.getBytes(ConstData.UTF8);
111
		OutputStream out = conn.getOutputStream();
112
		try {
113
			out.write(bypes);
114
			out.flush();
115
		} finally {
116
			out.close();
117
		}
118
		InputStream inStream = conn.getInputStream();
119
		return IoUtil.readStream(inStream, true);
120
	}
91 121
92 122
	private MobilePhoneResult parse(byte[] bytes) {
93 123
		return JsonService.fromJson(new InputStreamReader(new ByteArrayInputStream(bytes), ConstData.UTF8), MobilePhoneResult.class);
@ -109,10 +139,31 @@ public class MobilePhoneService {
109 139
		}
110 140
	}
111 141
	
142
	public void sendMarketMessage(String to, String contentTemplate) throws Exception {
143
		String dateStr = DateUtil.formatDateTime(System.currentTimeMillis());
144
		StringBuilder sb = new StringBuilder();
145
		sb.append("accountSid=").append(this.accountSid).append("&smsContent=").append(URLEncoder.encode(contentTemplate, "UTF-8")).append("&to=").append(to)
146
				.append("&timestamp=").append(dateStr).append("&sig=");
147
		String md5 = StringUtil.md5(this.accountSid + this.authToken + dateStr).toLowerCase(Locale.US);
148
		sb.append(md5);
149
		MobilePhoneResult mpr = this.parse(this.sendMarket(sb.toString()));
150
		if (!MobilePhoneResult.SUCCESS.equals(mpr.getRespCode())) {
151
			String reason = errorReason.get(mpr.getRespCode());
152
			if(reason==null) reason="未知错误,请联系技术客服。";
153
			throw new Exception(mpr.getRespCode()+"-"+reason);
154
		}
155
	}
156
	
112 157
	
113 158
	public static void main(String[] args) throws Exception{
114 159
		MobilePhoneService mps = new MobilePhoneService();
115
		mps.sendMessage("18601149468","【科袖科技】请输入验证码yzm,完成手机绑定。请于1分钟内正确输入验证码。如非本人操作,请忽略本短信。科袖科技","yzm","122234");
160
		try {
161
			mps.sendMarketMessage("13525007261", "【科袖】您收到了一个新的咨询,快去查看吧。http://a.app.qq.com/o/simple.jsp?pkgname=com.ekexiu.app退订回N");
162
			System.out.println("send success!");
163
		} catch (Exception e) {
164
			throw new JfwBaseException("send error!");
165
		}
166
		//mps.sendMessage("18601149468","【科袖科技】请输入验证码yzm,完成手机绑定。请于1分钟内正确输入验证码。如非本人操作,请忽略本短信。科袖科技","yzm","122234");
116 167
	}
117 168
118 169
	static {