|
8 | 8 | import org.apache.http.client.methods.HttpGet; |
9 | 9 | import org.apache.http.client.methods.HttpPost; |
10 | 10 | import org.apache.http.entity.StringEntity; |
| 11 | +import org.apache.http.protocol.HTTP; |
11 | 12 | import org.slf4j.Logger; |
12 | 13 | import org.slf4j.LoggerFactory; |
13 | 14 |
|
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | + |
14 | 17 | /** |
15 | 18 | * @description: UcloudHttp实现类 |
16 | 19 | * @author: codezhang |
@@ -53,10 +56,10 @@ public Object doPost(BaseRequestParam param, UcloudConfig config, |
53 | 56 | String httpPostParamString = ParamConstructor.getHttpPostParamString(param, config.getAccount()); |
54 | 57 | HttpPost post = new HttpPost(config.getApiServerAddr()); |
55 | 58 | //application/json |
56 | | - post.setHeader("Content-Type", "application/json; charset=utf-8"); |
| 59 | + post.setHeader(HTTP.CONTENT_TYPE, "application/json"); |
57 | 60 | //post |
58 | | - StringEntity entity = new StringEntity(httpPostParamString); |
59 | | - entity.setContentEncoding("UTF-8"); |
| 61 | + StringEntity entity = new StringEntity(httpPostParamString, StandardCharsets.UTF_8); |
| 62 | + entity.setContentType("application/json"); |
60 | 63 | post.setEntity(entity); |
61 | 64 | Http http = new Http(resultClass); |
62 | 65 | result = http.doHttp(post, handler, async(asyncFlag)); |
@@ -85,174 +88,4 @@ private Boolean async(Boolean... asyncFlag) { |
85 | 88 | } |
86 | 89 | return async; |
87 | 90 | } |
88 | | - |
89 | | -// @Override |
90 | | -// public Object doGet(BaseRequestParam param, UcloudConfig config, final UcloudHandler handler, |
91 | | -// Boolean... asyncFlag) throws Exception { |
92 | | -// Object result = null; |
93 | | -// try { |
94 | | -// // 创建http GET请求 |
95 | | -// String httpGetParamString = ParamConstructor.getHttpGetParamString(param, config.getAccount()); |
96 | | -// final HttpGet httpGet = new HttpGet(config.getApiServerAddr() + "?" + httpGetParamString); |
97 | | -// result = preHttp(httpGet, handler, asyncFlag); |
98 | | -// } catch (Exception e) { |
99 | | -// if (handler == null) { |
100 | | -// throw e; |
101 | | -// } else { |
102 | | -// handler.error(e); |
103 | | -// } |
104 | | -// } |
105 | | -// return result; |
106 | | -// } |
107 | | -// |
108 | | -// |
109 | | -// @Override |
110 | | -// public Object doPost(BaseRequestParam param, UcloudConfig config, UcloudHandler handler, Boolean... asyncFlag) |
111 | | -// throws Exception { |
112 | | -// Object result = null; |
113 | | -// try { |
114 | | -// // 创建http POST请求 |
115 | | -// String httpPostParamString = ParamConstructor.getHttpPostParamString(param, config.getAccount()); |
116 | | -// final HttpPost httpPost = new HttpPost(config.getApiServerAddr()); |
117 | | -// //application/json |
118 | | -// httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); |
119 | | -// //设置参数 |
120 | | -// StringEntity entity = new StringEntity(httpPostParamString); |
121 | | -// entity.setContentEncoding("UTF-8"); |
122 | | -// httpPost.setEntity(entity); |
123 | | -// result = preHttp(httpPost, handler, asyncFlag); |
124 | | -// } catch (Exception e) { |
125 | | -// if (handler == null) { |
126 | | -// throw e; |
127 | | -// } else { |
128 | | -// handler.error(e); |
129 | | -// } |
130 | | -// } |
131 | | -// return result; |
132 | | -// } |
133 | | -// |
134 | | -// private Object preHttp(HttpUriRequest request, UcloudHandler handler, Boolean... asyncFlag) throws Exception { |
135 | | -// //result 对象 |
136 | | -// Object responseResult = null; |
137 | | -// if (handler != null) { |
138 | | -// if (isSync(asyncFlag)) { |
139 | | -// // 同步回调 |
140 | | -// doHttp(request, handler); |
141 | | -// } else { |
142 | | -// // 异步回调 |
143 | | -// Thread thread = new Thread() { |
144 | | -// @Override |
145 | | -// public void run() { |
146 | | -// try { |
147 | | -// doHttp(request, handler); |
148 | | -// } catch (Exception e) { |
149 | | -// logger.error(e.getMessage()); |
150 | | -// } |
151 | | -// } |
152 | | -// }; |
153 | | -// thread.start(); |
154 | | -// } |
155 | | -// } else { |
156 | | -// // 同步 |
157 | | -// responseResult = doHttp(request, null); |
158 | | -// } |
159 | | -// return responseResult; |
160 | | -// } |
161 | | -// |
162 | | -// private BaseResponseResult doHttp(HttpUriRequest request, UcloudHandler handler) throws Exception { |
163 | | -// CloseableHttpResponse response = null; |
164 | | -// BaseResponseResult responseResult = null; |
165 | | -// // 创建Httpclient对象 |
166 | | -// final CloseableHttpClient client = HttpClients.createDefault(); |
167 | | -// try { |
168 | | -// request.addHeader("User-Agent", "Java/1.8.0_191 Java-SDK/0.8.2.7-release"); |
169 | | -// // 执行http get请求 |
170 | | -// logger.info("request line:{}",request.getRequestLine()); |
171 | | -// response = client.execute(request); |
172 | | -// if (response != null) { |
173 | | -// // 正常响应 |
174 | | -// String content = EntityUtils.toString(response.getEntity(), "UTF-8"); |
175 | | -// logger.info("response content:{}",content); |
176 | | -// if (statusOK(response)) { |
177 | | -// Gson gson = new Gson(); |
178 | | -// responseResult = gson.fromJson(content, resultClass); |
179 | | -// responseResult.setResponseContent(content); |
180 | | -// if (handler != null) { |
181 | | -// handleResult(handler, responseResult); |
182 | | -// } |
183 | | -// } else { |
184 | | -// // 非200则认为是个异常 |
185 | | -// if (handler != null) { |
186 | | -// handler.error(new HttpException(content)); |
187 | | -// } else { |
188 | | -// throw new HttpException(content); |
189 | | -// } |
190 | | -// } |
191 | | -// } else { |
192 | | -// handleException(handler, new NullPointerException("response is null")); |
193 | | -// } |
194 | | -// } catch (Exception e) { |
195 | | -// if (handler != null) { |
196 | | -// //异常 |
197 | | -// handler.error(e); |
198 | | -// } else { |
199 | | -// throw e; |
200 | | -// } |
201 | | -// } finally { |
202 | | -// try { |
203 | | -// if (response != null) { |
204 | | -// response.close(); |
205 | | -// } |
206 | | -// } catch (IOException e) { |
207 | | -// logger.error(e.getMessage()); |
208 | | -// } |
209 | | -// try { |
210 | | -// client.close(); |
211 | | -// } catch (IOException e) { |
212 | | -// logger.error(e.getMessage()); |
213 | | -// } |
214 | | -// } |
215 | | -// return responseResult; |
216 | | -// } |
217 | | -// |
218 | | -// private boolean isSync(Boolean[] asyncFlag) { |
219 | | -// return asyncFlag != null && asyncFlag.length > 0 && asyncFlag[0] != null && !asyncFlag[0]; |
220 | | -// } |
221 | | -// |
222 | | -// |
223 | | -// private void handleException(UcloudHandler handler, Exception e) { |
224 | | -// if (handler != null) { |
225 | | -// handler.error(e); |
226 | | -// } |
227 | | -// } |
228 | | -// |
229 | | -// private void handleResult(UcloudHandler handler, BaseResponseResult responseResult) { |
230 | | -// if (handler != null) { |
231 | | -// if (isSuccess(responseResult)) { |
232 | | -// handler.success(responseResult); |
233 | | -// } else { |
234 | | -// handler.failed(responseResult); |
235 | | -// } |
236 | | -// } |
237 | | -// } |
238 | | -// |
239 | | -// private boolean isSuccess(BaseResponseResult responseResult) { |
240 | | -// boolean success = false; |
241 | | -// if (responseResult != null) { |
242 | | -// success = responseResult.getRetCode() == 0; |
243 | | -// } |
244 | | -// return success; |
245 | | -// } |
246 | | -// |
247 | | -// |
248 | | -// private boolean statusOK(CloseableHttpResponse response) { |
249 | | -// boolean ok = false; |
250 | | -// if (response != null) { |
251 | | -// ok = response.getStatusLine().getStatusCode() == 200; |
252 | | -// } |
253 | | -// return ok; |
254 | | -// } |
255 | | - |
256 | | - |
257 | | - |
258 | 91 | } |
0 commit comments