在使用Java开发的时候,难免会用到别人写的若干的库,有些东西很小,但是很实用。
1.HttpClient HttpClient主要用来进行http调用,简单方便,还能创建连接池,提高并发数和节约资源。在调用时,容易发生Read time out 异常。捕获方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 private static String sendHttpPost (HttpPost httpPost) { CloseableHttpClient httpClient = null ; CloseableHttpResponse response = null ; String responseContent = null ; try { httpClient = getHttpClient(); httpPost.setConfig(requestConfig); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() >= 300 ) { throw new Exception ( "HTTP Request is not success, Response code is " + response.getStatusLine().getStatusCode()); } if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { responseContent = EntityUtils.toString(entity, CHARSET_UTF_8); EntityUtils.consume(entity); } }catch (SocketTimeoutException|ConnectTimeoutException ex){ logger.error("sendHttpPost请求超时" ); responseContent="请求超时" ; }catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null ) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; }
参考文章: 1.java.net.SocketTimeoutException: Read timed out 2.Java中CloseableHttpClient的超时设置和异常处理 3.HttpClient高并发下性能优化-http连接池 4.java.net.SocketTimeoutException: Read timed out
问题 (1) 生僻字乱码 今天使用HttpClient库发起Get请求,出现了一个生僻字“騄”,返回前台之后就显示了乱码。
但是在浏览器中直接打开这个get请求,发现并不会出现乱码。
本来以及是SpringBoot返回的问题,设置了produces = “application/json;charset=utf-8”,还是不行。
解决方法,因为原始请求为gb2312编码
(1) 新建转换方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public static String handleEntity (HttpEntity entity, String charset) throws IOException { if (entity == null ) return null ; ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024 ]; long count = entity.getContentLength(); long curCount = 0 ; int len = -1 ; InputStream is = entity.getContent(); while ((len = is.read(buffer)) != -1 ) { outStream.write(buffer, 0 , len); curCount += len; } byte [] data = outStream.toByteArray(); outStream.close(); is.close(); return new String (data, charset); }
(2) 将EntityUtils.toString(entity,”utf-8”),改为handleEntity(entity,”gbk”)
1 2 responseContent=handleEntity(entity,"gbk" );
参考文章: 1.【Spring】解决 @ResponseBody返回中文字符串乱码以及生僻字被强制以unicode方式显示问题 2.Http请求返回结果报UnsupportedCharsetException (我使用了这个代码进行了EntityUtils.toString) 3.踩坑记:httpComponents 的 EntityUtils 4.EntityUtils.tostring 中文乱码 5.SpringBoot的restTemplate整合HttpClient连接池及配置 (这个是连接池相关的内容) 6.HttpClient在传参和返回结果的中文乱码问题 (这里有说明如何进行发送端配置请求编码格式) 7.java HttpClientUtil (这是我HttpClinetUtil的主要参考代码,说是参考,其实都是Copy过来的,也就是简单的做了下修改,至于源码到底是不是正确的,以及为什么正确的,我都没有深究)
2.org.apache.commons.lang lang包可以用 RandomStringUtils.randomAlphanumeric(16) 生成16位的随机字符串,还包括了其他的有用的类和工具,常用的有ArrayUtils和StringUtils,具体可以参见 参考文章(1)
1 2 3 4 5 6 <dependency > <groupId > org.apache.commons</groupId > <artifactId > commons-lang3</artifactId > <version > 3.9</version > </dependency >
参考文章: 1.常用jar包之commons-lang使用 2.生成随机字符串(三种方式)
3.JustAuth 我在研究使用 jeecg-boot 的时候,遇到了这个授权认证的组件,集成了超过13种的国内外通用平台的认证方式。
参考文章: 1.vue + justauth 实现前后端分离下的第三方登录