lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
package com.kuanzhai.driver.utils;
 
 
import com.google.gson.Gson;
 
import java.io.*;
import java.net.*;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 融云及时通讯工具类
 */
public class RongYunUtil {
 
    //    @Value("${rongyun.app_key}")
    private static String app_key = "pwe86ga5p9ar6";
 
    //    @Value("${rongyun.app_secret}")
    private static String app_secret = "FvpwfHqk5o";
 
 
    /**
     * 发送系统消息
     *
     * @param fromUserId 发送人id
     * @param toUserId   接收人id
     * @param content    消息内容
     * @return
     */
    public static boolean pushSystem(String fromUserId, String toUserId, String content) {
        String url = "http://api-cn.ronghub.com/message/system/publish.json";
        Map<String, String> jsonObject = new HashMap();
        jsonObject.put("fromUserId", "1");
        jsonObject.put("toUserId", "122");
        jsonObject.put("content", "推送消息内容");
        String s = pushHttp(url, new Gson().toJson(jsonObject));
        jsonObject = new Gson().fromJson(s, Map.class);
        if (jsonObject.get("code").toString() == "200") {
            return true;
        }
        return false;
    }
 
 
    /**
     * 请求接口
     *
     * @param path
     * @param json
     * @return
     */
    public static String pushHttp(String path, String json) {
        String nonce = String.valueOf(Double.valueOf(Math.random() * 1000000.0D).intValue());
        String timeMillis = String.valueOf(System.currentTimeMillis());
        String signature = getSha1(app_secret + nonce + timeMillis);
 
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(1000 * 30);
            connection.setRequestProperty("Host", "api-cn.ronghub.com");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("App-Key", app_key);
            connection.setRequestProperty("Nonce", nonce);
            connection.setRequestProperty("Timestamp", timeMillis);
            connection.setRequestProperty("Signature", signature);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.connect();
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(json);
            outputStream.flush();
            outputStream.close();
 
            InputStream inputStream = connection.getInputStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            int len;
            byte[] bytes = new byte[1024];
            while ((len = dataInputStream.read(bytes)) != -1) {
                stream.write(bytes, 0, len);
            }
            return stream.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getSha1(String str) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }
}