【Java SE】http请求HttpClient
文档:https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/fundamentals.html
依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
简单例子
public String getJsonDataFromUrl(String url, String method) { String json = ""; // ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池 // ConnectTimeout 连接建立时间,三次握手完成时间 // SocketTimeout 等待服务端响应 数据传输过程中数据包之间间隔的最大时 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000).setConnectionRequestTimeout(60000).setSocketTimeout(60000).build(); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { if ("get".equals(method.toLowerCase())) { HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); response = httpClient.execute(httpGet); } else { List<NameValuePair> list = new ArrayList<>(); if (url.contains("?")) { int index = url.indexOf("?"); String paramStr = url.substring(index + 1); url = url.substring(0, index); String[] itemArray = paramStr.split("&"); for (String item : itemArray) { String[] pairArray = item.split("="); list.add(new BasicNameValuePair(pairArray[0], pairArray[1])); } } HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(list, Consts.UTF_8)); response = httpClient.execute(httpPost); } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity, "UTF-8"); } } } catch (Exception e) { e.printStackTrace(); log.error("获取数据异常", e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); log.error("关闭CloseableHttpResponse异常", e); } } } return json; }