HttpUtil.java
1.97 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
package com.subsidy.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
/**
* http请求
*/
public class HttpUtil {
/**
* GET 请求
* @param params
* @param url
* @return
*/
public static JSONObject get(Map<String, Object> params, String url) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
return object;
}
/**
* POST 请求
* @param params
* @param url
* @return
*/
public static JSONObject post(Map<String, Object> params, String url) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, params, String.class);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
return object;
}
/**
* POST 请求 带Token
*
* @param param
* @param token
* @param url
* @return
*/
public static JSONObject sendPost(Map<String, Object> param, String token, String url, Object obj) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", MediaType.ALL_VALUE);
headers.add("Authorization", "Bearer " + token);
HttpEntity<String> formEntity = new HttpEntity<String>(param.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, formEntity, String.class, obj);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
return object;
}
}