# 接口客户端: 点我下载
# 接口客户端使用示例(Java):
/**
* 猫酷开放平台接入示例
* Class WebApi
* @package tool\mallcoo
*/
public class WebApi {
private static WebApi webApi = null;
private String publicKey = "";
private String privateKey = "";
private String AppID = "";
public static WebApi getIns() {
if (webApi == null) {
webApi = new WebApi();
}
return webApi;
}
/**
* 方式1:
* 使用HttpsURLConnection进行接口请求
* @param requestUrl 请求地址
* @param jsonParameter 请求json参数
* @return
*/
public String requestPost(String requestUrl, String jsonParameter) {
return httpsRequest(requestUrl, "POST", jsonParameter);
}
private String httpsRequest(String requestUrl, String requestMethod, String jsonParameter) {
StringBuffer buffer = null;
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm = { new MyX509TrustManager() };
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-type", "application/json");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestMethod(requestMethod);
setHeadr(conn, jsonParameter);
conn.setRequestProperty("Content-Length", "0");
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.write("".getBytes("UTF-8"), 0, 0);
os.writeBytes(jsonParameter);
os.flush();
os.close();
conn.setSSLSocketFactory(ssf);
conn.connect();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
if (buffer != null) {
return buffer.toString();
} else {
return "";
}
}
/**
* 设置header参数
* @param conn
* @param jsonParameter
*/
private void setHeadr(HttpsURLConnection conn, String jsonParameter) {
if (jsonParameter == null) {
jsonParameter = "";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String dateStr = simpleDateFormat.format(new Date());
String timeStamp = dateStr;
String encryptString = "{publicKey:" + publicKey + ",timeStamp:" + timeStamp + ",data:" + jsonParameter + ",privateKey:" + privateKey + "}";
String sign = MD5.md5EncryptTo16(encryptString);
conn.setRequestProperty("AppID", AppID);
conn.setRequestProperty("TimeStamp", timeStamp);
conn.setRequestProperty("PublicKey", publicKey);
conn.setRequestProperty("Sign", sign);
}
/**
* 方式2:
* 使用HttpClient进行接口请求
* @param url 请求地址
* @param jsonParameter 请求json参数
* @return
*/
public String doPost(String url, String jsonParameter) {
String charset = "UTF-8";
String result = null;
try {
SSLClient httpClient = new SSLClient();
HttpPost httpPost = new HttpPost(url);
setHeadr(httpPost, jsonParameter);
// 解决中文乱码问题
StringEntity stringEntity = new StringEntity(jsonParameter, charset);
stringEntity.setContentEncoding(charset);
httpPost.setEntity(stringEntity);
System.out.println("Executing request " + httpPost.getRequestLine());
// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {//
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
result = httpClient.execute(httpPost, responseHandler);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 设置header信息
* @param httpPost
* @param jsonParameter
*/
private void setHeadr(HttpPost httpPost, String jsonParameter) {
if (jsonParameter == null) {
jsonParameter = "";
}
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setHeader("Connection", "Keep-Alive");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String dateStr = simpleDateFormat.format(new Date());
String timeStamp = dateStr;
String encryptString = "{publicKey:" + publicKey + ",timeStamp:" + timeStamp + ",data:" + jsonParameter + ",privateKey:" + privateKey + "}";
//md5加密
String sign = MD5.md5EncryptTo16(encryptString);
httpPost.setHeader("AppID", AppID);
httpPost.setHeader("TimeStamp", timeStamp);
httpPost.setHeader("PublicKey", publicKey);
httpPost.setHeader("Sign", sign);
}
}
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
168
169
170
171
172
173
174
175
176
177
178
179
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
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Class Main
*/
public class Main {
public static void main(String[] args) {
System.out.println("==================不带参调用======================");
String url = "https://openapi10-t.mallcoo.cn/Mall/Commercial/V1/GetList/";
String result = WebApi.getIns().requestPost(url, "");
System.out.println("http方式返回值"+result);
result = WebApi.getIns().doPost(url, "");
System.out.println("HttpClient方式返回值:" + result);
System.out.println("==================带参调用======================");
url = "https://openapi10-t.mallcoo.cn/Shop/V1/GetList/";
JSONObject object = new JSONObject();
object.put("PageIndex", 1);
object.put("PageSize", 10);
object.put("FloorID", 0);
object.put("CommercialTypeID", 1);
result = WebApi.getIns().requestPost(url, object.toString());
System.out.println("http方式返回值"+result);
result = WebApi.getIns().doPost(url, object.toString());
System.out.println("HttpClient方式返回值:" + result);
}
}
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
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