OpenApiDemo.java
3.9 KB
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
/*
* COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2018.
* ALL RIGHTS RESERVED.
*
* No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
* on any form or by any means, electronic, mechanical, photocopying, recording,
* or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
*
* Amendment History:
*
* Date By Description
* ------------------- ----------- -------------------------------------------
* 2018年5月18日 yaojianping Create the class
* http://www.jimilab.com/
*/
package com.jimi.openapi;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.jimi.openapi.util.SignUtils;
/**
* @FileName OpenApiDemo.java
* @Description:
*
* @Date 2018年5月18日 下午5:53:13
* @author yaojianping
* @version 1.0
*/
public class OpenApiDemo {
private static final String openapi_url = "http://open.aichezaixian.com/route/rest";
// 申请来的appKey和appSecret
private static final String app_key = "申请的key";
private static final String app_secret = "申请的secret";
public static void main(String[] args) {
Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", "application/x-www-form-urlencoded");
Map<String, String> paramMap = new HashMap<String, String>();
// 公共参数
paramMap.put("app_key", app_key);
paramMap.put("v", "1.0");
paramMap.put("timestamp", getCurrentDate());
paramMap.put("sign_method", "md5");
paramMap.put("format", "json");
paramMap.put("method", "jimi.oauth.token.get");
// 私有参数
paramMap.put("user_id", "您的账号");
paramMap.put("user_pwd_md5", DigestUtils.md5Hex("您的密码"));
paramMap.put("expires_in", "120");
// 计算签名
String sign = "";
try {
sign = SignUtils.signTopRequest(paramMap, app_secret, "md5");
paramMap.put("sign", sign);
} catch (IOException e) {
System.err.println(e);
}
sendPost(headerMap, paramMap);
}
private static void sendPost(Map<String, String> headerMap, Map<String, String> paramMap) {
try {
HttpPost post = new HttpPost(openapi_url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (String key : paramMap.keySet()) {
list.add(new BasicNameValuePair(key, paramMap.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
if (null != headerMap) {
post.setHeaders(assemblyHeader(headerMap));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity, "utf-8"));
} catch (IOException e) {
System.err.println(e);
}
}
/**
* 组装头部信息
*
* @param headers
* @return
*/
private static Header[] assemblyHeader(Map<String, String> headers) {
Header[] allHeader = new BasicHeader[headers.size()];
int i = 0;
for (String str : headers.keySet()) {
allHeader[i] = new BasicHeader(str, headers.get(str));
i++;
}
return allHeader;
}
public static String getCurrentDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = formatter.format(new Date());
return result;
}
}