[测评系统]--测评系统核心代码库
吴祝攀
2024-06-14 499b1d27da156ddd25c7adb58a5601805d8149fe
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
package com.ots.project.tool.sms;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
 
public class InternationalMessage {
    
    public final static String INTERNATIONAL_SMS_URL = "http://api2.santo.cc/submit";
    
    public final static String INTERNATIONAL_SMS_COMMAND = "MT_REQUEST";
    
    public static String SendPost(String url, String command, String cpid, String cppwd, String da, String sm,
                                  String sa) throws Exception {
        HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager());
        PostMethod method = new PostMethod();
        try {
            URI base = new URI(url, false);
            method.setURI(new URI(base, "", false));
            method.setRequestBody(new NameValuePair[]{
                    new NameValuePair("command", command),
                    new NameValuePair("cpid", cpid),
                    new NameValuePair("cppwd", cppwd),
                    new NameValuePair("da", da),
                    new NameValuePair("sm", sm),
                    new NameValuePair("sa", sa)});
            HttpMethodParams params = new HttpMethodParams();
            params.setContentCharset("UTF-8");
            method.setParams(params);
            int result = client.executeMethod(method);
            if (result == HttpStatus.SC_OK) {
                InputStream in = method.getResponseBodyAsStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = in.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                in.close();
                baos.close();
                return URLDecoder.decode(baos.toString(), "UTF-8");
            } else {
                throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
            }
        } finally {
            method.releaseConnection();
        }
    }
    
    public static String SendGet(String url, String command, String cpid, String cppwd, String da, String sm,
                                 String sa) throws Exception {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod();
        try {
            URI base = new URI(url, false);
            method.setURI(new URI(base, "", false));
            method.setQueryString(new NameValuePair[]{
                    new NameValuePair("command", command),
                    new NameValuePair("cpid", cpid),
                    new NameValuePair("cppwd", cppwd),
                    new NameValuePair("da", da),
                    new NameValuePair("sm", sm),
                    new NameValuePair("sa", sa)});
            int result = client.executeMethod(method);
            if (result == HttpStatus.SC_OK) {
                InputStream in = method.getResponseBodyAsStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = in.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                in.close();
                baos.close();
                return URLDecoder.decode(baos.toString(), "UTF-8");
            } else {
                throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
            }
        } finally {
            method.releaseConnection();
        }
    }
    
    public static String query(String url, String cpid, String cppwd) throws Exception {
        HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
        GetMethod method = new GetMethod();
        try {
            URI base = new URI(url, false);
            method.setURI(base);
            method.setQueryString(new NameValuePair[]{
                    new NameValuePair("cpid", cpid),
                    new NameValuePair("cppwd", cppwd)});
            int result = client.executeMethod(method);
            if (result == HttpStatus.SC_OK) {
                InputStream in = method.getResponseBodyAsStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = in.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                in.close();
                baos.close();
                return URLDecoder.decode(baos.toString(), "UTF-8");
            } else {
                throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
            }
        } finally {
            method.releaseConnection();
        }
    }
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }
    public static String decodeHexStr(String hexStr) {
        String realStr = null;
        if (hexStr != null) {
            try {
                byte[] data = hexStr2ByteArr(hexStr);
                realStr = new String(data, "GBK");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return realStr;
    }
}