package com.ots.project.tool.sms;
|
import com.alibaba.fastjson.JSON;
|
import com.ots.common.utils.bean.BeanUtils;
|
import com.thoughtworks.xstream.XStream;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.security.MessageDigest;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
|
@Slf4j
|
@Component
|
public class ZjunShortMessageImpl {
|
private final static String PATTERN = "yyyyMMddhhmmss";
|
public final static String RET_STATUS_SUCCESS = "Success";
|
public final static String RET_STATUS_FAIL = "Faild";
|
XStream xstream = new XStream();
|
String accountId = "12128";
|
String account = "NDA";
|
String password = "nda@2019";
|
String xml = "http://agent.izjun.cn";
|
Date date = new Date();
|
SimpleDateFormat sf = new SimpleDateFormat(PATTERN);
|
String dateStr = sf.format(date);
|
String str = account + password + dateStr;
|
String sign = MD5Encode(str);
|
public static void main(String[] args) throws Exception {
|
XStream xstream = new XStream();
|
xstream.alias("returnsms", Returnsms.class);
|
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><returnsms> <returnstatus>Success</returnstatus> <message>ok</message> <remainpoint>87</remainpoint> <taskID>68920759</taskID> <successCounts>1</successCounts></returnsms>";
|
Returnsms returnsms = (Returnsms) xstream.fromXML(xml);
|
System.out.println(JSON.toJSON(returnsms));
|
ZjunShortMessageImpl impl = new ZjunShortMessageImpl();
|
Returnsms sendmsg = impl.sendmsg("13611452175", "【掌骏传媒】", "http://139.199.11.114/exam-stu/#/demography/ed860660-95a7-4feb-aab9-f56b1eff1a3c/12/index");
|
System.out.println(JSON.toJSON(sendmsg));
|
Returnsms returnsms1 = impl.checkYUE();
|
System.out.println(JSON.toJSON(returnsms1));
|
}
|
public Returnsms sendmsg(String mobile, String smsSign, String content) throws Exception {
|
String urlString = xml + "/v2sms.aspx?";
|
String params = "action=send&userid=" + accountId + "×tamp=" + dateStr + "&sign=" + sign + "&mobile=" + mobile
|
+ "&content=" + smsSign + content + "&sendTime=&extno=";
|
return send(urlString, params);
|
}
|
public Returnsms checkYUE() throws Exception {
|
String urlString = xml + "/v2sms.aspx?";
|
String params = "action=overage&userid=" + accountId + "×tamp=" + dateStr + "&sign=" + sign;
|
return send(urlString, params);
|
}
|
public List<Statusbox> getReport() {
|
String urlString = xml + "/v2statusApi.aspx?";
|
String params = "action=query&userid=" + accountId + "×tamp=" + dateStr + "&sign=" + sign;
|
return sendForReport(urlString, params);
|
}
|
public Returnsms GetMo() throws Exception {
|
String urlString = xml + "/v2callApi.aspx?";
|
String params = "action=query&userid=" + accountId + "×tamp=" + dateStr + "&sign=" + sign;
|
return send(urlString, params);
|
}
|
public final static String MD5Encode(String s) {
|
try {
|
byte[] btInput = s.getBytes("utf-8");
|
MessageDigest mdInst = MessageDigest.getInstance("MD5");
|
mdInst.update(btInput);
|
byte[] md = mdInst.digest();
|
StringBuffer sb = new StringBuffer();
|
for (int i = 0; i < md.length; i++) {
|
int val = ((int) md[i]) & 0xff;
|
if (val < 16) {
|
sb.append("0");
|
}
|
sb.append(Integer.toHexString(val));
|
}
|
return sb.toString();
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
private Returnsms send(String urlString, String params) {
|
Returnsms returnsms = new Returnsms();
|
BufferedReader in = null;
|
HttpURLConnection hp = null;
|
try {
|
URL url = new URL(urlString);
|
hp = (HttpURLConnection) url.openConnection();
|
byte[] b = params.getBytes("utf-8");
|
hp.setRequestMethod("POST");
|
hp.setDoOutput(true);
|
hp.setDoInput(true);
|
OutputStream out = hp.getOutputStream();
|
out.write(b);
|
out.close();
|
in = new BufferedReader(new InputStreamReader(hp.getInputStream(), "utf-8"));
|
xstream.alias("returnsms", Returnsms.class);
|
Object fromXML = xstream.fromXML(in);
|
BeanUtils.copyProperties(fromXML, returnsms);
|
return returnsms;
|
} catch (Exception e) {
|
returnsms.setReturnstatus(Returnsms.RET_FAILD);
|
returnsms.setMessage(e.getMessage());
|
} finally {
|
if (Objects.nonNull(in)) {
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (Objects.nonNull(hp)) {
|
hp.disconnect();
|
}
|
}
|
return returnsms;
|
}
|
private List<Statusbox> sendForReport(String urlString, String params) {
|
BufferedReader in = null;
|
HttpURLConnection hp = null;
|
try {
|
URL url = new URL(urlString);
|
hp = (HttpURLConnection) url.openConnection();
|
byte[] b = params.getBytes("utf-8");
|
hp.setRequestMethod("POST");
|
hp.setDoOutput(true);
|
hp.setDoInput(true);
|
OutputStream out = hp.getOutputStream();
|
out.write(b);
|
out.close();
|
in = new BufferedReader(new InputStreamReader(hp.getInputStream(), "utf-8"));
|
XStream xstream = new XStream();
|
xstream.setClassLoader(ZjunShortMessageImpl.class.getClassLoader());
|
xstream.alias("returnsms", ArrayList.class);
|
xstream.alias("statusbox", Statusbox.class);
|
return (List<Statusbox>) xstream.fromXML(in);
|
} catch (IOException ioe) {
|
log.error("sendForReport error:{}", ioe.getMessage(), ioe);
|
} finally {
|
if (Objects.nonNull(in)) {
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (Objects.nonNull(hp)) {
|
hp.disconnect();
|
}
|
}
|
return null;
|
}
|
}
|