欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

调用第三方请求获取数据

时间:2023-06-30

public String sendGetRequest(String reqURL, Map paramMap, String decodeCharset){ // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; //处理URL的一些传参 if (!paramMap.isEmpty()) { for (String key : paramMap.keySet()) { if (reqURL.indexOf('?') == -1) { reqURL += "?" + key + "=" + paramMap.get(key); } else { reqURL += "&" + key + "=" + paramMap.get(key); } } } try { HttpGet httpGet = new HttpGet(reqURL); // 执行请求 response = httpclient.execute(httpGet); response.setHeader("Content-Type", "application/json"); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。