Python 官方文档:入门教程 => 点击学习
当采用HttpClient httpClient = HttpClients.createDefault() 实例化的时候。会导致Address already in use的异常。
当采用HttpClient httpClient = HttpClients.createDefault() 实例化的时候。会导致Address already in use的异常。
信息: I/O exception (java.net.BindException) caught when processing request to {}->http:/ private static CloseableHttpClient getCloseableHttpClient() { CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(poolConnManager) .setRetryHandler(httpRequestRetryHandler) .build(); return httpClient; } private static Map<String, Object> buildResultMap(CloseableHttpResponse response, HttpEntity entity) throws IOException { Map<String, Object> result; result = new HashMap<>(2); result.put(KEY_STATUS_CODE, response.getStatusLine().getStatusCode()); //status code if (entity != null) { result.put(KEY_CONTENT, EntityUtils.toString(entity, "UTF-8")); //message content } return result; } public static Map<String, Object> postJSON(String url, String message) { Map<String, Object> result = null; CloseableHttpClient httpClient = getCloseableHttpClient(); HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null; try { httpPost.setHeader("Accept", "application/json;charset=UTF-8"); httpPost.setHeader("Content-Type", "application/json"); StringEntity stringEntity = new StringEntity(message); stringEntity.setContentType("application/json;charset=UTF-8"); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = buildResultMap(response, entity); } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { EntityUtils.consume(response.getEntity()); response.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } }
到此这篇关于Java httpClient连接池支持多线程高并发的实现的文章就介绍到这了,更多相关Java httpClient连接池多线程高并发内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java httpClient连接池支持多线程高并发的实现
本文链接: https://www.lsjlt.com/news/133430.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0