admin
2019-03-13 33b4ed2bbf28ec16b66e552680f56a691a4e908d
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
package com.yeshi.fanli.util.push;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.yeshi.utils.HttpUtil;
 
import com.yeshi.fanli.exception.push.VIVOPushException;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
/**
 * VIVO的推送帮助
 * 
 * @author Administrator
 *
 */
public class VIVOPushUtil {
 
    /**
     * POST请求
     * 
     * @param url
     * @param body
     * @param headers
     * @return
     */
    private static String post(String url, String body, Map<String, String> headers) {
        HttpClient httpClient = new HttpClient();
        PostMethod pm = new PostMethod(url);
        try {
            pm.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 
        if (headers != null) {
            Iterator<String> its = headers.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                pm.setRequestHeader(key, headers.get(key));
            }
        }
 
        try {
            httpClient.executeMethod(pm);
            return pm.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取授权码
     * 
     * @param appId
     * @param appKey
     * @param appSecret
     * @return
     * @throws VIVOPushException
     */
    public static String getAuthToken(String appId, String appKey, String appSecret) throws VIVOPushException {
        JSONObject params = new JSONObject();
        long timestamp = System.currentTimeMillis();
        params.put("appId", appId);
        params.put("appKey", appKey);
        params.put("timestamp", timestamp);
        params.put("sign", StringUtil.Md5(appId + appKey + timestamp + appSecret));
        String result = post("https://host:port/message/auth", params.toString(), null);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("result") != 0)
            throw new VIVOPushException(resultJson.optInt("result"), resultJson.optString("desc"));
        return resultJson.optString("authToken");
    }
    
    
 
 
}