[测评系统]--测评系统核心代码库
linzhijie
2021-03-11 84fea994d2db7dc313ad1774f34eb12a45f8d6e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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://sms.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("1351274544", "【掌骏传媒】", "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 + "&timestamp=" + 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 + "&timestamp=" + dateStr + "&sign=" + sign;
        return send(urlString, params);
    }
    public List<Statusbox> getReport() { 
        String urlString = xml + "/v2statusApi.aspx?";
        String params = "action=query&userid=" + accountId + "&timestamp=" + dateStr + "&sign=" + sign;
        return sendForReport(urlString, params);
    }
    public Returnsms GetMo() throws Exception { 
        String urlString = xml + "/v2callApi.aspx?";
        String params = "action=query&userid=" + accountId + "&timestamp=" + 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;
    }
}