httpClient笔记
NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
報(bào)錯(cuò)信息:org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
解決方案:
參考:
stackoverflow
Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
使用HttpClient,大量報(bào)出Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended的WARN日志,定位到HttpClient的源碼如下:
public abstract class HttpMethodBase implements HttpMethod {public byte[] getResponseBody() throws IOException {if (responseBody == null) {InputStream instream = getResponseBodyAsStream();if (instream != null) {long contentLength = getResponseContentLength();if (contentLength > 2147483647L) {throw new IOException("Content too large to be buffered: " + contentLength + " bytes");}int limit = getParams().getIntParameter("http.method.response.buffer.warnlimit", 1048576);if (contentLength == -1L || contentLength > (long) limit) {LOG.warn("Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.");}LOG.debug("Buffering response body");ByteArrayOutputStream outstream = new ByteArrayOutputStream(contentLength <= 0L ? 4096: (int) contentLength);byte buffer[] = new byte[4096];int len;while ((len = instream.read(buffer)) > 0) {outstream.write(buffer, 0, len);}outstream.close();setResponseStream(null);responseBody = outstream.toByteArray();}}return responseBody;} }報(bào)WARN的條件(contentLength == -1) || (contentLength > limit),即返回的HTTP頭沒(méi)有指定contentLength,或contentLength大于上限(默認(rèn)1M)。如果能確定返回結(jié)果的大小對(duì)程序沒(méi)有顯著影響,這個(gè)WARN就可以忽略,可在日志配置中把HttpClient的日志級(jí)別調(diào)到ERROR。
不過(guò)這都是忽略潛在的問(wèn)題,并沒(méi)有解決問(wèn)題。
HttpClient建議使用InputStream getResponseBodyAsStream()代替byte[] getResponseBody()。對(duì)于返回結(jié)果很大或無(wú)法預(yù)知的情況,使用InputStreamgetResponseBodyAsStream(),避免byte[] getResponseBody()可能帶來(lái)的內(nèi)存耗盡問(wèn)題。
解決方案,將stream轉(zhuǎn)化為string:
private String convert(InputStream inputStream) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));StringBuilder sb = new StringBuilder();String str;while ((str = br.readLine()) != null) {sb.append(str);}return sb.toString(); }ConnectException: Connection refused
使用企業(yè)微信推送消息時(shí)遇到的問(wèn)題,具體的報(bào)錯(cuò)信息:
java.lang.Exception: org.apache.http.conn.HttpHostConnectException: Connect to qyapi.weixin.qq.com:443 [qyapi.weixin.qq.com/81.69.87.29, qyapi.weixin.qq.com/81.69.54.213] failed: Connection refused報(bào)錯(cuò)行at com.xy.cloudiview.common.util.HttpUtil.doGet(HttpUtil.java:49)at com.xy.cloudiview.common.util.SendWeChatUtil.getToken(SendWeChatUtil.java:193)at com.xy.cloudiview.common.util.SendWeChatUtil.sendWeChat(SendWeChatUtil.java:57)at com.xy.cloudiview.datasetsubscript.business.service.impl.TableWarnServiceImpl.sendWeChat(TableWarnServiceImpl.java:330)at com.xy.cloudiview.datasetsubscript.business.service.impl.TableWarnServiceImpl.sendMsg(TableWarnServiceImpl.java:417)at com.xy.cloudiview.datasetsubscript.business.service.impl.TableWarnServiceImpl.executeTableWarnJob(TableWarnServiceImpl.java:99)at com.xy.cloudiview.datasetsubscript.business.xxljob.IviewTableWarnJobHandler.execute(IviewTableWarnJobHandler.java:45)at com.ppdai.job.core.thread.JobThread.run(JobThread.java:142) Caused by: org.apache.http.conn.HttpHostConnectException: Connect to qyapi.weixin.qq.com:443 [qyapi.weixin.qq.com/81.69.87.29, qyapi.weixin.qq.com/81.69.54.213] failed: Connection refusedat org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)at com.xy.cloudiview.common.util.HttpUtil.doGet(HttpUtil.java:39)... 7 common frames omitted Caused by: java.net.ConnectException: Connection refusedat java.net.PlainSocketImpl.socketConnect(Native Method)at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)at java.net.Socket.connect(Socket.java:589)at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:368)at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)... 17 common frames omittedHttpUtil.doGet方法定義:
public static String doGet(String url) throws Exception {// 創(chuàng)建Httpclient對(duì)象CloseableHttpClient httpclient = HttpClients.createDefault();// 創(chuàng)建http GET請(qǐng)求HttpGet httpGet = new HttpGet(url);CloseableHttpResponse response = null;String content = "";try {// 執(zhí)行請(qǐng)求response = httpclient.execute(httpGet);// 判斷返回狀態(tài)是否為200if (response.getStatusLine().getStatusCode() == 200) {//請(qǐng)求體內(nèi)容content = EntityUtils.toString(response.getEntity(), "UTF-8");} else {log.error("doget error, url:{}, return code:{}", url,response.getStatusLine().getStatusCode());}} catch (Exception e) {// 報(bào)錯(cuò)行throw new Exception(e);} finally {if (response != null) {response.close();}//相當(dāng)于關(guān)閉瀏覽器httpclient.close();}return content; }沒(méi)有什么意義的參考:
- 偶現(xiàn)訪問(wèn) qyapi.weixin.qq.com被拒絕的情況?
- how-to-fix-java-net-connectexception-connection-refused-connect-in-java
參考
- HttpClient警告
總結(jié)
以上是生活随笔為你收集整理的httpClient笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 学生使用计算机的管理制度,学生使用计算机
- 下一篇: 微信小程序--监听对象属性变化