HttpClientUtils.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * FileName:HttpClientUtils.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import com.alibaba.fastjson.JSON;
  21. import com.alibaba.fastjson.JSONObject;
  22. import com.google.common.collect.ImmutableMap;
  23. import org.apache.http.HttpEntity;
  24. import org.apache.http.HttpResponse;
  25. import org.apache.http.NameValuePair;
  26. import org.apache.http.client.config.RequestConfig;
  27. import org.apache.http.client.entity.UrlEncodedFormEntity;
  28. import org.apache.http.client.methods.CloseableHttpResponse;
  29. import org.apache.http.client.methods.HttpGet;
  30. import org.apache.http.client.methods.HttpPost;
  31. import org.apache.http.client.utils.URLEncodedUtils;
  32. import org.apache.http.config.Registry;
  33. import org.apache.http.config.RegistryBuilder;
  34. import org.apache.http.conn.socket.ConnectionSocketFactory;
  35. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  36. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  37. import org.apache.http.entity.ContentType;
  38. import org.apache.http.entity.StringEntity;
  39. import org.apache.http.entity.mime.MultipartEntityBuilder;
  40. import org.apache.http.entity.mime.content.FileBody;
  41. import org.apache.http.entity.mime.content.StringBody;
  42. import org.apache.http.impl.client.CloseableHttpClient;
  43. import org.apache.http.impl.client.HttpClientBuilder;
  44. import org.apache.http.impl.client.HttpClients;
  45. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  46. import org.apache.http.message.BasicNameValuePair;
  47. import org.apache.http.ssl.SSLContexts;
  48. import org.apache.http.util.EntityUtils;
  49. import org.apache.logging.log4j.LogManager;
  50. import org.apache.logging.log4j.Logger;
  51. import javax.net.ssl.SSLContext;
  52. import java.io.*;
  53. import java.net.URL;
  54. import java.net.URLConnection;
  55. import java.nio.charset.StandardCharsets;
  56. import java.util.ArrayList;
  57. import java.util.HashMap;
  58. import java.util.List;
  59. import java.util.Map;
  60. /**
  61. * HttpClient 搬砖工具类
  62. *
  63. *
  64. */
  65. public class HttpClientUtils {
  66. private static Logger logger = LogManager.getLogger(HttpClientUtils.class);
  67. private HttpClientUtils() {
  68. throw new IllegalAccessError("工具类不能实例化");
  69. }
  70. // private static BasicHttpClientConnectionManager connectionManager = null;
  71. private static PoolingHttpClientConnectionManager connectionManager = null;
  72. // private static HttpClientBuilder httpClientBuilder=null;
  73. private static RequestConfig requestConfig = RequestConfig.custom()
  74. .setSocketTimeout(30*1000)
  75. .setConnectTimeout(8000)
  76. .setConnectionRequestTimeout(6000)
  77. .build();
  78. static {
  79. SSLContext sslcontext = SSLContexts.createSystemDefault();
  80. // Create a registry of custom connection socket factories for supported //
  81. //protocol schemes.
  82. Registry<ConnectionSocketFactory> socketFactoryRegistry =
  83. RegistryBuilder.<ConnectionSocketFactory>create()
  84. .register("http", PlainConnectionSocketFactory.INSTANCE)
  85. .register("https", new SSLConnectionSocketFactory(sslcontext)) .build();
  86. // 使用基本的Httpclient链接器
  87. // connectionManager=new
  88. // BasicHttpClientConnectionManager(socketFactoryRegistry);
  89. connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
  90. // connectionManager = new PoolingHttpClientConnectionManager();
  91. connectionManager.setMaxTotal(1000);
  92. connectionManager.setDefaultMaxPerRoute(200);// 每个路由最大的请求数量
  93. // httpClientBuilder =
  94. // HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
  95. // HttpHost localhost = new HttpHost("http://www.baidu.com",80);
  96. // connectionManager.setMaxPerRoute(new HttpRoute(localhost), 200);
  97. }
  98. public static CloseableHttpClient getHttpClient() {
  99. return getHttpClientBuilder().build();
  100. }
  101. public static CloseableHttpClient getHttpClient(SSLContext sslContext) {
  102. return getHttpClientBuilder(sslContext).build();
  103. }
  104. public static HttpClientBuilder getHttpClientBuilder() {
  105. return HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
  106. // .setConnectionManagerShared(true);
  107. }
  108. public static HttpClientBuilder getHttpClientBuilder(SSLContext sslContext) {
  109. if (sslContext != null) {
  110. return getHttpClientBuilder().setSslcontext(sslContext);
  111. } else {
  112. return getHttpClientBuilder();
  113. }
  114. }
  115. /**
  116. * post 请求
  117. *
  118. * @param httpUrl
  119. * 请求地址
  120. * @param sslContext
  121. * ssl证书信息
  122. * @return
  123. */
  124. public static String sendHttpPost(String httpUrl, SSLContext sslContext) {
  125. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  126. return sendHttpPost(httpPost, sslContext);
  127. }
  128. /**
  129. * 发送 post请求
  130. *
  131. * @param httpUrl
  132. * 地址
  133. */
  134. public static String sendHttpPost(String httpUrl) {
  135. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  136. return sendHttpPost(httpPost, null);
  137. }
  138. /**
  139. * 发送 post请求
  140. *
  141. * @param httpUrl
  142. * 地址
  143. * @param params
  144. * 参数(格式:key1=value1&key2=value2)
  145. */
  146. public static String sendHttpPost(String httpUrl, String params) {
  147. return sendHttpPost(httpUrl, params, null);
  148. }
  149. /**
  150. * 发送 post请求
  151. *
  152. * @param httpUrl
  153. * 地址
  154. * @param params
  155. * 参数(格式:key1=value1&key2=value2)
  156. * @param sslContext
  157. * ssl证书信息
  158. */
  159. public static String sendHttpPost(String httpUrl, String params, SSLContext sslContext) {
  160. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  161. try {
  162. // 设置参数
  163. StringEntity stringEntity = new StringEntity(params, "UTF-8");
  164. stringEntity.setContentType("application/x-www-form-urlencoded");
  165. httpPost.setEntity(stringEntity);
  166. } catch (Exception e) {
  167. logger.error(e.getMessage(), e);
  168. }
  169. return sendHttpPost(httpPost, sslContext);
  170. }
  171. /**
  172. * 发送 post请求
  173. *
  174. * @param httpUrl
  175. * 地址
  176. * @param maps
  177. * 参数
  178. */
  179. public static String sendHttpPost(String httpUrl, Map<String, String> maps) {
  180. return sendHttpPost(httpUrl, maps, null);
  181. }
  182. public static String sendHttpPost(String httpUrl, List<String> maps) {
  183. return sendHttpPost(httpUrl, maps, null);
  184. }
  185. /**
  186. * 发送 post请求
  187. *
  188. * @param httpUrl
  189. * 地址
  190. * @param maps
  191. * 参数
  192. * @param sslContext
  193. * ssl证书信息
  194. */
  195. public static String sendHttpPost(String httpUrl, Map<String, String> maps, SSLContext sslContext) {
  196. HttpPost httpPost = wrapHttpPost(httpUrl, maps);
  197. return sendHttpPost(httpPost, null);
  198. }
  199. public static String sendHttpPost(String httpUrl, List<String> maps, SSLContext sslContext) {
  200. HttpPost httpPost = wrapHttpPost(httpUrl, maps);
  201. return sendHttpPost(httpPost, null);
  202. }
  203. public static String sendHttpPostWithHeader(
  204. String httpUrl, Map<String, String> maps, Map<String, String> headers) {
  205. HttpPost httpPost = wrapHttpPost(httpUrl, maps);
  206. for (Map.Entry<String, String> entry : headers.entrySet()) {
  207. String key = entry.getKey();
  208. String value = entry.getValue();
  209. httpPost.setHeader(key, value);
  210. }
  211. return sendHttpPost(httpPost, null);
  212. }
  213. /**
  214. * 封装获取HttpPost方法
  215. *
  216. * @param httpUrl
  217. * @param maps
  218. * @return
  219. */
  220. public static HttpPost wrapHttpPost(String httpUrl, Map<String, String> maps) {
  221. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  222. // 创建参数队列
  223. List<NameValuePair> nameValuePairs = new ArrayList<>();
  224. for (Map.Entry<String, String> m : maps.entrySet()) {
  225. nameValuePairs.add(new BasicNameValuePair(m.getKey(), m.getValue()));
  226. }
  227. try {
  228. httpPost.setHeader("Content-Type", "application/json");
  229. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  230. } catch (Exception e) {
  231. logger.error(e.getMessage(), e);
  232. }
  233. return httpPost;
  234. }
  235. /**
  236. * 封装获取HttpPost方法
  237. *
  238. * @param httpUrl
  239. * @param entity
  240. * @return
  241. */
  242. public static HttpPost wrapHttpPost(String httpUrl, List<String> entity) {
  243. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  244. try {
  245. httpPost.setHeader("Content-Type", "application/json");
  246. JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(entity));
  247. httpPost.setEntity(new StringEntity(jsonObject.toString(), StandardCharsets.UTF_8));
  248. } catch (Exception e) {
  249. logger.error(e.getMessage(), e);
  250. }
  251. return httpPost;
  252. }
  253. /**
  254. * 发送 post请求(带文件)
  255. *
  256. * @param httpUrl
  257. * 地址
  258. * @param file
  259. * 附件,名称和File对应
  260. */
  261. public static String sendHttpPost(String httpUrl, File file) {
  262. return sendHttpPost(httpUrl, ImmutableMap.of( "media", file), null, null);
  263. }
  264. /**
  265. * 发送 post请求(带文件)
  266. *
  267. * @param httpUrl
  268. * 地址
  269. * @param file
  270. * 附件,名称和File对应
  271. * @param maps
  272. * 参数
  273. */
  274. public static String sendHttpPost(String httpUrl, File file, Map<String, String> maps) {
  275. return sendHttpPost(httpUrl, ImmutableMap.of( "media", file), maps, null);
  276. }
  277. /**
  278. * 发送 post请求(带文件),默认 files 名称数组.
  279. *
  280. * @param httpUrl
  281. * 地址
  282. * @param fileLists
  283. * 附件
  284. * @param maps
  285. * 参数
  286. */
  287. public static String sendHttpPost(String httpUrl, List<File> fileLists, Map<String, String> maps) {
  288. return sendHttpPost(httpUrl, fileLists, maps, null);
  289. }
  290. /**
  291. * 发送 post请求(带文件)
  292. *
  293. * @param httpUrl
  294. * 地址
  295. * @param fileMap
  296. * 附件,名称和File对应
  297. * @param maps
  298. * 参数
  299. */
  300. public static String sendHttpPost(String httpUrl, Map<String, File> fileMap, Map<String, String> maps) {
  301. return sendHttpPost(httpUrl, fileMap, maps, null);
  302. }
  303. /**
  304. * 发送 post请求(带文件),默认 files 名称数组.
  305. *
  306. * @param httpUrl
  307. * 地址
  308. * @param fileLists
  309. * 附件
  310. * @param maps
  311. * 参数
  312. * @param sslContext
  313. * ssl证书信息
  314. */
  315. public static String sendHttpPost(String httpUrl, List<File> fileLists, Map<String, String> maps,
  316. SSLContext sslContext) {
  317. Map<String, File> fileMap = new HashMap<>();
  318. if (fileLists==null||fileLists.isEmpty()) {
  319. for (File file : fileLists) {
  320. fileMap.put("media", file);
  321. }
  322. }
  323. return sendHttpPost(httpUrl, fileMap, maps, sslContext);
  324. }
  325. /**
  326. * 发送 post请求(带文件)
  327. *
  328. * @param httpUrl
  329. * 地址
  330. * @param fileMap
  331. * 附件,名称和File对应
  332. * @param maps
  333. * 参数
  334. * @param sslContext
  335. * ssl证书信息
  336. */
  337. public static String sendHttpPost(String httpUrl, Map<String, File> fileMap, Map<String, String> maps,
  338. SSLContext sslContext) {
  339. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  340. MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
  341. if (null != maps) {
  342. for (Map.Entry<String, String> m : maps.entrySet()) {
  343. meBuilder.addPart(m.getKey(), new StringBody(m.getValue(), ContentType.TEXT_PLAIN));
  344. }
  345. }
  346. if (null != fileMap) {
  347. for (Map.Entry<String, File> m : fileMap.entrySet()) {
  348. FileBody fileBody = new FileBody(m.getValue());
  349. meBuilder.addPart(m.getKey(), fileBody);
  350. }
  351. }
  352. HttpEntity reqEntity = meBuilder.build();
  353. httpPost.setEntity(reqEntity);
  354. return sendHttpPost(httpPost, sslContext);
  355. }
  356. /**
  357. * 发送Post请求
  358. *
  359. * @param httpPost
  360. * @return
  361. */
  362. public static String sendHttpPost(HttpPost httpPost) {
  363. return sendHttpPost(httpPost, null);
  364. }
  365. /**
  366. * 发送Post请求
  367. *
  368. * @param httpPost
  369. * @param sslConext
  370. * ssl证书信息
  371. * @return
  372. */
  373. public static String sendHttpPost(HttpPost httpPost, SSLContext sslConext) {
  374. httpPost.setConfig(requestConfig);
  375. httpPost.setHeader("Content-Type", "application/json");
  376. CloseableHttpClient httpClient = getHttpClient(sslConext);
  377. CloseableHttpResponse response = null;
  378. HttpEntity entity = null;
  379. String responseContent = null;
  380. try {
  381. // 执行请求
  382. response = httpClient.execute(httpPost);
  383. entity = response.getEntity();
  384. responseContent = EntityUtils.toString(entity, "UTF-8");
  385. } catch (Exception e) {
  386. logger.error(e.getMessage(), e);
  387. } finally {
  388. try {
  389. // 关闭连接,释放资源
  390. if (entity != null) {
  391. EntityUtils.consumeQuietly(entity); // 会自动释放连接
  392. }
  393. if (response != null) {
  394. response.close();
  395. }
  396. } catch (Exception e) {
  397. logger.error(e.getMessage(), e);
  398. }
  399. }
  400. return responseContent;
  401. }
  402. /**
  403. * 发送 get请求
  404. *
  405. * @param httpUrl
  406. */
  407. public static String sendHttpGet(String httpUrl, Map<String, String> params) {
  408. List<NameValuePair> nameValuePairs = new ArrayList<>();
  409. for (Map.Entry<String, String> m : params.entrySet()) {
  410. nameValuePairs.add(new BasicNameValuePair(m.getKey(), m.getValue()));
  411. }
  412. try {
  413. String paramsUri = URLEncodedUtils.format(nameValuePairs, "UTF-8");
  414. if(httpUrl.indexOf("?") > 0) {
  415. httpUrl = httpUrl + "&" + paramsUri;
  416. } else {
  417. httpUrl = httpUrl + "?" + paramsUri;
  418. }
  419. } catch (Exception e) {
  420. logger.error(e.getMessage(), e);
  421. }
  422. HttpGet httpGet = new HttpGet(httpUrl.replaceAll(" ", "%20"));// 创建get请求
  423. return sendHttpGet(httpGet, null);
  424. }
  425. // public static String sendHttpGet(String httpUrl) {
  426. //
  427. // }
  428. /**
  429. * 发送 get请求
  430. *
  431. * @param httpUrl
  432. * @param sslConext
  433. * ssl证书信息
  434. */
  435. public static String sendHttpGet(String httpUrl, SSLContext sslConext) {
  436. HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
  437. return sendHttpGet(httpGet, sslConext);
  438. }
  439. public static String sendHttpGet(String httpUrl) {
  440. return sendHttpGet(new HttpGet(httpUrl), null);
  441. }
  442. /**
  443. * 发送Get请求
  444. *
  445. * @param httpGet
  446. * @return
  447. */
  448. public static String sendHttpGet(HttpGet httpGet) {
  449. return sendHttpGet(httpGet, null);
  450. }
  451. /**
  452. * 发送Get请求
  453. *
  454. * @param httpGet
  455. * @param sslConext
  456. * ssl证书信息
  457. * @return
  458. */
  459. public static String sendHttpGet(HttpGet httpGet, SSLContext sslConext) {
  460. CloseableHttpClient httpClient = getHttpClient(sslConext);
  461. CloseableHttpResponse response = null;
  462. HttpEntity entity = null;
  463. String responseContent = null;
  464. try {
  465. // 执行请求
  466. response = httpClient.execute(httpGet);
  467. entity = response.getEntity();
  468. responseContent = EntityUtils.toString(entity, "UTF-8");
  469. } catch (Exception e) {
  470. logger.error(e.getMessage(), e);
  471. } finally {
  472. try {
  473. // 关闭连接,释放资源
  474. if (entity != null) {
  475. EntityUtils.consumeQuietly(entity); // 会自动释放连接
  476. }
  477. if (response != null) {
  478. response.close();
  479. }
  480. } catch (Exception e) {
  481. logger.error(e.getMessage(), e);
  482. }
  483. }
  484. return responseContent;
  485. }
  486. /**
  487. * 发送 get请求
  488. *
  489. * @param httpUrl
  490. * 请求路径
  491. * @param headers
  492. * 请求头参数
  493. * @return
  494. */
  495. public static String sendHttpHeaderGet(String httpUrl, Map<String, String> headers) {
  496. HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
  497. for (Map.Entry<String, String> entry : headers.entrySet()) {
  498. String key = entry.getKey().toString();
  499. String value = entry.getValue().toString();
  500. httpGet.setHeader(key, value);
  501. }
  502. return sendHttpGet(httpGet, null);
  503. }
  504. /**
  505. * Get 下载文件
  506. *
  507. * @param httpUrl
  508. * @param file
  509. * @return
  510. */
  511. public static File sendHttpGetFile(String httpUrl, File file) {
  512. if (file == null) {
  513. return null;
  514. }
  515. HttpGet httpGet = new HttpGet(httpUrl);
  516. CloseableHttpClient httpClient = getHttpClient();
  517. CloseableHttpResponse response = null;
  518. HttpEntity entity = null;
  519. InputStream inputStream = null;
  520. FileOutputStream fileOutputStream = null;
  521. try {
  522. // 执行请求
  523. response = httpClient.execute(httpGet);
  524. entity = response.getEntity();
  525. inputStream = entity.getContent();
  526. fileOutputStream = new FileOutputStream(file);
  527. int len = 0;
  528. byte[] buf = new byte[1024];
  529. while ((len = inputStream.read(buf, 0, 1024)) != -1) {
  530. fileOutputStream.write(buf, 0, len);
  531. }
  532. } catch (Exception e) {
  533. logger.error(e.getMessage(), e);
  534. } finally {
  535. try {
  536. if (fileOutputStream != null) {
  537. fileOutputStream.close();
  538. }
  539. if (inputStream != null) {
  540. inputStream.close();
  541. }
  542. // 关闭连接,释放资源
  543. if (entity != null) {
  544. EntityUtils.consumeQuietly(entity); // 会自动释放连接
  545. }
  546. if (response != null) {
  547. response.close();
  548. }
  549. } catch (Exception e) {
  550. logger.error(e.getMessage(), e);
  551. }
  552. }
  553. return file;
  554. }
  555. /**
  556. * Post 下载文件
  557. * @param httpUrl
  558. * @param maps
  559. * @param file
  560. * @return
  561. */
  562. public static File sendHttpPostFile(String httpUrl, Map<String, String> maps, File file) {
  563. if (file == null) {
  564. return null;
  565. }
  566. HttpPost httpPost=wrapHttpPost(httpUrl, maps);
  567. CloseableHttpClient httpClient = getHttpClient();
  568. CloseableHttpResponse response = null;
  569. HttpEntity entity = null;
  570. InputStream inputStream = null;
  571. FileOutputStream fileOutputStream = null;
  572. try {
  573. // 执行请求
  574. response = httpClient.execute(httpPost);
  575. entity = response.getEntity();
  576. inputStream = entity.getContent();
  577. fileOutputStream = new FileOutputStream(file);
  578. int len = 0;
  579. byte[] buf = new byte[1024];
  580. while ((len = inputStream.read(buf, 0, 1024)) != -1) {
  581. fileOutputStream.write(buf, 0, len);
  582. }
  583. } catch (Exception e) {
  584. logger.error(e.getMessage(), e);
  585. } finally {
  586. try {
  587. if (fileOutputStream != null) {
  588. fileOutputStream.close();
  589. }
  590. if (inputStream != null) {
  591. inputStream.close();
  592. }
  593. // 关闭连接,释放资源
  594. if (entity != null) {
  595. EntityUtils.consumeQuietly(entity); // 会自动释放连接
  596. }
  597. if (response != null) {
  598. response.close();
  599. }
  600. } catch (Exception e) {
  601. logger.error(e.getMessage(), e);
  602. }
  603. }
  604. return file;
  605. }
  606. public static JSONObject sendHttpPostCarInfo(List<String> carIds){
  607. //定义接收数据
  608. JSONObject result = new JSONObject();
  609. String url = "http://47.101.51.219:10000/api/Carrier/RealtimePositon";
  610. HttpPost httpPost = new HttpPost(url);
  611. CloseableHttpClient client = HttpClients.createDefault();
  612. //请求参数转JOSN字符串
  613. String carIdsStr = carIds.toString();
  614. StringEntity entity = new StringEntity(carIdsStr, "UTF-8");
  615. entity.setContentEncoding("UTF-8");
  616. entity.setContentType("application/json");
  617. httpPost.setEntity(entity);
  618. try {
  619. HttpResponse response = client.execute(httpPost);
  620. if (response.getStatusLine().getStatusCode() == 200) {
  621. result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
  622. }
  623. } catch (IOException e) {
  624. e.printStackTrace();
  625. result.put("error", "连接错误!");
  626. }
  627. return result;
  628. }
  629. }