HttpTemplate.java
6.69 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
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
package com.lhcredit.common.utils.http;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.project.business.TianYC.entity.param.RequestParams;
import com.lhcredit.project.business.TianYC.entity.param.SFParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
@Slf4j
@RequiredArgsConstructor
public class HttpTemplate {
@Value("${dataCenters.key}")
private String key;
// 令牌信息
@Value("${dataCenters.token}")
private String token;
// private final RestTemplate restTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public String httpGetWithHeaders(String url) {
log.info("===============请求地址信息:{}", url);
// HttpHeaders headers = new HttpHeaders();
// headers.set("token", token);
// HttpEntity<?> requestEntity = new HttpEntity<>(headers);
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
// return response.getBody();
return HttpRequest.get(url).header("token", token).execute().body();
}
// public String httpPost(String url, String name) {
// HttpHeaders headers = new HttpHeaders();
// headers.set("token", token);
// return restTemplate.postForEntity(url, name, String.class).getBody();
// }
public JSONObject getResult(String url) {
try {
String string = httpGetWithHeaders(url);
return JSONObject.parseObject(string);
} catch (Exception e) {
log.error("============调用接口失败{}", e.getMessage(), e);
return getErr("调用接口失败", "5000");
}
}
public JSONObject getPost(String url,JSONObject j){
j.put("key",key);
String result = HttpRequest.post(url)
.header("Token", token)
.header(Header.CONTENT_TYPE,"application/json")
.body(j.toString())
.timeout(20000)//超时,毫秒
.execute().body();
return JSONObject.parseObject(result);
}
// 返回异常信息
public JSONObject getErr(String msg, String code) {
JSONObject result = new JSONObject();
result.put("msg", msg);
result.put("code", code);
return result;
}
// 组装url参数信息
public String getTycUrl(String url, RequestParams requestParams) {
url = getString(url, requestParams.getName(), requestParams.getPageNum(), requestParams.getPageSize());
if (ObjectUtil.isNotNull(requestParams.getSearchType())) {
url = url + "&searchType=" + requestParams.getSearchType();
}
if (StringUtils.isNotBlank(requestParams.getAppDateEnd())) {
url = url + "&appDateBegin=" + requestParams.getAppDateEnd();
}
if (StringUtils.isNotBlank(requestParams.getTmClass())) {
url = url + "&tmClass=" + requestParams.getTmClass();
}
if (StringUtils.isNotBlank(requestParams.getAppDateBegin())) {
url = url + "&appDateBegin=" + requestParams.getAppDateBegin();
}
if (ObjectUtil.isNotNull(requestParams.getStatus())) {
url = url + "&status=" + requestParams.getStatus();
}
if (StringUtils.isNotBlank(requestParams.getPubDateBegin())) {
url = url + "&pubDateBegin=" + requestParams.getPubDateBegin();
}
if (StringUtils.isNotBlank(requestParams.getPubDateEnd())) {
url = url + "&pubDateEnd=" + requestParams.getPubDateEnd();
}
if (ObjectUtil.isNotNull(requestParams.getPatentType())) {
url = url + "&patentType=" + requestParams.getPatentType();
}
if (ObjectUtil.isNotNull(requestParams.getStatusCode())) {
url = url + "&statusCode=" + requestParams.getStatusCode();
}
if (StringUtils.isNotBlank(requestParams.getWord())) {
url = url + "&word=" + HttpUtils.urlEncode(requestParams.getWord());
}
if (StringUtils.isNotBlank(requestParams.getHumanName())) {
url = url + "&humanName=" + HttpUtils.urlEncode(requestParams.getHumanName());
}
if (StringUtils.isNotBlank(requestParams.getStartTime())) {
url = url + "&startTime=" + HttpUtils.urlEncode(requestParams.getStartTime());
}
if (StringUtils.isNotBlank(requestParams.getEndTime())) {
url = url + "&endTime=" + HttpUtils.urlEncode(requestParams.getEndTime());
}
if (StringUtils.isNotBlank(requestParams.getTags())) {
url = url + "&tags=" + HttpUtils.urlEncode(requestParams.getTags());
}
if (StringUtils.isNotBlank(requestParams.getHid())) {
url = url + "&hid=" + HttpUtils.urlEncode(requestParams.getHid());
}
if (StringUtils.isNotBlank(requestParams.getCid())) {
url = url + "&cid=" + HttpUtils.urlEncode(requestParams.getCid());
}
if (StringUtils.isNotBlank(requestParams.getAssistanceId())) {
url = url + "&assistanceId=" + requestParams.getAssistanceId();
}
return url;
}
// 组装url参数信息
public String getSFUrl(String url, SFParams sfParams) {
url = getString(url, sfParams.getName(), sfParams.getPageNum(), sfParams.getPageSize());
if (StringUtils.isNotBlank(sfParams.getId())) {
url = url + "&id=" + sfParams.getId();
}
if (StringUtils.isNotBlank(sfParams.getQueryParam())) {
url = url + "&queryParam=" + sfParams.getQueryParam();
}
if (StringUtils.isNotBlank(sfParams.getType())) {
url = url + "&type=" + sfParams.getType();
}
System.out.println(url);
return url;
}
private String getString(String url, String name, Integer pageNum, Integer pageSize) {
url = url + "?key=" + key;
if (StringUtils.isNotBlank(name)) {
url = url + "&name=" + name;
}
if (ObjectUtil.isNotNull(pageNum) && ObjectUtil.isNotNull(pageSize)) {
url = url + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
}
return url;
}
}