开发者社区> 问答> 正文

获取 httpclient 响应流的出错? 400 报错

获取 httpclient 响应流的出错? 400 报错

public InputStream execute(HttpRequestBase httpRequestBase,Set<Integer> acceptStatus){ CloseableHttpResponse response = null; StatusLine status = null; try { response = httpClient.execute(httpRequestBase); status = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (acceptStatus == null) { return entity.getContent(); }else { int statusCode = status.getStatusCode(); if (isAcceptStatus(acceptStatus, statusCode)) { return entity.getContent(); }else { throw new RuntimeException("请求出错"); } } }catch(ClientProtocolException e){ throw new RuntimeException("请求出错"); } catch (IOException e) { throw new RuntimeException("请求出错"); }finally { if(response != null){ try { response.close(); } catch (IOException e) { // output exception } } if(httpRequestBase != null ){ httpRequestBase.releaseConnection(); } } }

如上代码,如果是响应流则字节数比较少(如少于 100 kb),可以顺利执行的,如果获取的较多字节的响应流,会出现 socket closed exception。

问题是,如果获取完整的响应流??



展开
收起
爱吃鱼的程序员 2020-05-30 23:54:27 489 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    可能是还没有读取完返回的数据socket就已经超时自动关闭了。你把httpClient的soTimeout设大点再试试呢

    2020-05-30 23:54:28
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载