admin
2019-10-10 e19ce4be094d93f68bdb6ee1c28e9caa502bf2c4
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
package com.yeshi.fanli.util.vip;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
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.yeshi.utils.StringUtil;
 
import net.sf.json.JSONObject;
 
/**
 * TODO 未完成 唯品会接口
 * 
 * @author Administrator
 *
 */
public class VipApiUtil {
 
    private final static String appKey = "f9e7f22f";
    private final static String appSecret = "9B2291352497FAF42B2DF44BFCF62316";
 
    /**
     * 获取签名
     * 
     * @param params
     * @return
     */
    private static String getSign(Map<String, String> systemParams, JSONObject taskParams) {
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            String value = systemParams.get(key);
            list.add(key + value);
        }
        Collections.sort(list);
        String source = "";
        for (String st : list)
            source += st;
        source += taskParams.toString();
        return StringUtil.MD5Hmac(source, appSecret);
    }
 
    private static Map<String, String> getSystemParams(String service, String method) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("service", service);
        params.put("method", method);
        params.put("version", "1.0");
        params.put("timestamp", System.currentTimeMillis() / 1000 + "");
        params.put("format", "json");
        params.put("appKey", appKey);
        return params;
    }
 
    /**
     * 基础请求
     * 
     * @param service
     * @param method
     * @param taskParams
     * @return
     */
    private static String baseRequest(String service, String method, Map<String, String> taskParams) {
        Map<String, String> systemParams = getSystemParams(service, method);
        JSONObject json = new JSONObject();
        for (Iterator<String> its = taskParams.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            String value = taskParams.get(key);
            json.put(key, value);
        }
        JSONObject root = new JSONObject();
        root.put("request",json);
        
        String sign = getSign(systemParams, root);
        systemParams.put("sign", sign);
        String baseUrl = "https://gw.vipapis.com";
        baseUrl += "?";
        for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            String value = "";
            try {
                value = URLEncoder.encode(systemParams.get(key), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            baseUrl += key + "=" + value + "&";
        }
        baseUrl = baseUrl.endsWith("&") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
        String result = post(baseUrl, root.toString());
        return result;
    }
 
    @SuppressWarnings("deprecation")
    private static String post(String url, String body) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        method.setRequestBody(body);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static void search() {
        Map<String, String> taskParams = new HashMap<>();
        taskParams.put("keyword","笔记本");
        taskParams.put("page", "1");
        taskParams.put("pageSize", "20");
        taskParams.put("requestId", System.currentTimeMillis() + "");
        // taskParams.put("priceStart", "");
        // taskParams.put("priceEnd", "");
        String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "query", taskParams);
        System.out.println(result);
    }
 
}