|
@@ -1,87 +1,87 @@
|
1
|
|
-//package com.example.civilizedcity.common;
|
2
|
|
-//
|
3
|
|
-//import org.springframework.http.*;
|
4
|
|
-//import org.springframework.stereotype.Component;
|
5
|
|
-//import org.springframework.web.client.RestTemplate;
|
6
|
|
-//
|
7
|
|
-//import java.io.UnsupportedEncodingException;
|
8
|
|
-//import java.net.URLDecoder;
|
9
|
|
-//import java.util.*;
|
10
|
|
-//
|
11
|
|
-//@Component
|
12
|
|
-//public class HttpUtils {
|
13
|
|
-// public String get(String url) throws Exception {
|
14
|
|
-// RestTemplate restTemplate = new RestTemplate();
|
15
|
|
-// HttpHeaders headers = new HttpHeaders();
|
16
|
|
-// headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
17
|
|
-// HttpEntity<String> entity = new HttpEntity<String>(headers);
|
18
|
|
-//
|
19
|
|
-// Map<String, Object> urlAndParams = parseURL(url);
|
20
|
|
-// String formatURL = (String) urlAndParams.get("url");
|
21
|
|
-// Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
22
|
|
-//
|
23
|
|
-// ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.GET, entity, String.class, params);
|
24
|
|
-// return resp.getBody();
|
25
|
|
-// }
|
26
|
|
-//
|
27
|
|
-// public String post(String url, Map<String, String> header, String body) throws Exception {
|
28
|
|
-// RestTemplate restTemplate = new RestTemplate();
|
29
|
|
-// HttpHeaders headers = new HttpHeaders();
|
30
|
|
-// MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
31
|
|
-// headers.setContentType(type);
|
32
|
|
-// headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
33
|
|
-// headers.setAll(header);
|
34
|
|
-// HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
35
|
|
-//
|
36
|
|
-// Map<String, Object> urlAndParams = parseURL(url);
|
37
|
|
-// String formatURL = (String) urlAndParams.get("url");
|
38
|
|
-// Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
39
|
|
-//
|
40
|
|
-// ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.POST, entity, String.class, params);
|
41
|
|
-// return resp.getBody();
|
42
|
|
-// }
|
43
|
|
-//
|
44
|
|
-// // RestTemplate 不能正常的处理 querystring, 必须按照约定的方式处理
|
45
|
|
-// private Map<String, Object> parseURL(String url) {
|
46
|
|
-// Map<String, Object> res = new HashMap<>();
|
47
|
|
-//
|
48
|
|
-// String[] parts = url.split("\\?");
|
49
|
|
-// String baseURL = parts[0];
|
50
|
|
-//
|
51
|
|
-// if (parts.length < 2) {
|
52
|
|
-// res.put("url", baseURL);
|
53
|
|
-// res.put("params", new HashMap<>());
|
54
|
|
-// return res;
|
55
|
|
-// }
|
56
|
|
-//
|
57
|
|
-//
|
58
|
|
-// String[] fields = parts[1].split("&");
|
59
|
|
-// Map<String, String> params = new HashMap<>();
|
60
|
|
-// List<String> searchList = new ArrayList<>();
|
61
|
|
-// for (String field : fields) {
|
62
|
|
-// String[] pairs = field.split("=");
|
63
|
|
-// if (2 != pairs.length) {
|
64
|
|
-// continue;
|
65
|
|
-// }
|
66
|
|
-//
|
67
|
|
-// try {
|
68
|
|
-// String key = URLDecoder.decode(pairs[0].trim(), "UTF-8");
|
69
|
|
-// String val = URLDecoder.decode(pairs[1].trim(), "UTF-8");
|
70
|
|
-//
|
71
|
|
-// // 转化为 "foo={foo}" 这种字符串
|
72
|
|
-// searchList.add(String.format("%s={%s}", key, key));
|
73
|
|
-// // 把 foo 对应的值存储起来
|
74
|
|
-// params.put(key, val);
|
75
|
|
-// } catch (UnsupportedEncodingException e) {
|
76
|
|
-// e.printStackTrace();
|
77
|
|
-// }
|
78
|
|
-// }
|
79
|
|
-//
|
80
|
|
-// // url 格式类似 http://foo.org/bar?name={name}
|
81
|
|
-// String formatURL = baseURL + "?" + String.join("&", searchList);
|
82
|
|
-// res.put("url", formatURL);
|
83
|
|
-// res.put("params", params);
|
84
|
|
-//
|
85
|
|
-// return res;
|
86
|
|
-// }
|
87
|
|
-//}
|
|
1
|
+package com.example.civilizedcity.common;
|
|
2
|
+
|
|
3
|
+import org.springframework.http.*;
|
|
4
|
+import org.springframework.stereotype.Component;
|
|
5
|
+import org.springframework.web.client.RestTemplate;
|
|
6
|
+
|
|
7
|
+import java.io.UnsupportedEncodingException;
|
|
8
|
+import java.net.URLDecoder;
|
|
9
|
+import java.util.*;
|
|
10
|
+
|
|
11
|
+@Component
|
|
12
|
+public class HttpUtils {
|
|
13
|
+ public String get(String url) throws Exception {
|
|
14
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
15
|
+ HttpHeaders headers = new HttpHeaders();
|
|
16
|
+ headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
|
17
|
+ HttpEntity<String> entity = new HttpEntity<String>(headers);
|
|
18
|
+
|
|
19
|
+ Map<String, Object> urlAndParams = parseURL(url);
|
|
20
|
+ String formatURL = (String) urlAndParams.get("url");
|
|
21
|
+ Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
|
22
|
+
|
|
23
|
+ ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.GET, entity, String.class, params);
|
|
24
|
+ return resp.getBody();
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public String post(String url, Map<String, String> header, String body) throws Exception {
|
|
28
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
29
|
+ HttpHeaders headers = new HttpHeaders();
|
|
30
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
31
|
+ headers.setContentType(type);
|
|
32
|
+ headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
|
33
|
+ headers.setAll(header);
|
|
34
|
+ HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
|
35
|
+
|
|
36
|
+ Map<String, Object> urlAndParams = parseURL(url);
|
|
37
|
+ String formatURL = (String) urlAndParams.get("url");
|
|
38
|
+ Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
|
39
|
+
|
|
40
|
+ ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.POST, entity, String.class, params);
|
|
41
|
+ return resp.getBody();
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ // RestTemplate 不能正常的处理 querystring, 必须按照约定的方式处理
|
|
45
|
+ private Map<String, Object> parseURL(String url) {
|
|
46
|
+ Map<String, Object> res = new HashMap<>();
|
|
47
|
+
|
|
48
|
+ String[] parts = url.split("\\?");
|
|
49
|
+ String baseURL = parts[0];
|
|
50
|
+
|
|
51
|
+ if (parts.length < 2) {
|
|
52
|
+ res.put("url", baseURL);
|
|
53
|
+ res.put("params", new HashMap<>());
|
|
54
|
+ return res;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+ String[] fields = parts[1].split("&");
|
|
59
|
+ Map<String, String> params = new HashMap<>();
|
|
60
|
+ List<String> searchList = new ArrayList<>();
|
|
61
|
+ for (String field : fields) {
|
|
62
|
+ String[] pairs = field.split("=");
|
|
63
|
+ if (2 != pairs.length) {
|
|
64
|
+ continue;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ try {
|
|
68
|
+ String key = URLDecoder.decode(pairs[0].trim(), "UTF-8");
|
|
69
|
+ String val = URLDecoder.decode(pairs[1].trim(), "UTF-8");
|
|
70
|
+
|
|
71
|
+ // 转化为 "foo={foo}" 这种字符串
|
|
72
|
+ searchList.add(String.format("%s={%s}", key, key));
|
|
73
|
+ // 把 foo 对应的值存储起来
|
|
74
|
+ params.put(key, val);
|
|
75
|
+ } catch (UnsupportedEncodingException e) {
|
|
76
|
+ e.printStackTrace();
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ // url 格式类似 http://foo.org/bar?name={name}
|
|
81
|
+ String formatURL = baseURL + "?" + String.join("&", searchList);
|
|
82
|
+ res.put("url", formatURL);
|
|
83
|
+ res.put("params", params);
|
|
84
|
+
|
|
85
|
+ return res;
|
|
86
|
+ }
|
|
87
|
+}
|