QQ_hoverer 2021-06-04 10:22 采纳率: 0%
浏览 91

源代码如下,爆: Attempted read from closed stream怎么解决?

 private static String getRemoteCallLogStr(HttpUriRequest httpUriRequest, HttpResponse httpResponse, long start, String method, Exception ex, String siteName) {
        String startTime = DateFormatUtils.format(new Date(start), "yyyy-MM-dd'T'HH:mm:ss.SSS");
        String path = "";
        String targetUrl = "";
        String uri = "";
        String targetHost = "";
        String requestheader = "";
        String responseHeader = "";
        String responseBody = "";
        Integer responseStatus = null;
        if (!Objects.isNull(httpUriRequest)) {
            if (!Objects.isNull(httpUriRequest.getURI())) {
                path = httpUriRequest.getURI().getPath();
                targetUrl = httpUriRequest.getURI().toString();
                targetHost = httpUriRequest.getURI().getHost();
                uri = httpUriRequest.getURI().toString();
            }
            requestheader = JSON.toJSONString(httpUriRequest.getAllHeaders());
        }
        if (!Objects.isNull(httpResponse)) {
            if (!Objects.isNull(httpResponse.getStatusLine())) {
                responseStatus = httpResponse.getStatusLine().getStatusCode();
            }
            HttpEntity entity = httpResponse.getEntity();
            if (!Objects.isNull(entity)) {
                try {
                    //entity实体流保存缓冲区,否则只能操作一次流就会关闭 ,BufferedHttpEntity可以多次读取流
                    //entity = new BufferedHttpEntity(entity);
                    responseBody = URLEncodedUtils.parse(entity).toString();
                } catch (IOException e) {
                    LogUtils.error(e, log, "io转换错误");
                }
            }
            responseHeader = JSON.toJSONString(httpResponse.getAllHeaders());
        }
        RemoteCallLog content = RemoteCallLog.builder()
                .type("remote")
                .loggerName(HttpsUtils.class.getName())
                .level(ex == null ? "INFO" : "ERROR")
                .logId(LogUtils.getLogId())
                .time(startTime)
                .hostName(EnvironmentUtil.getHostName())
                .targetMethod(path)
                .targetUrl(targetUrl)
                .requestId(LogUtils.initRequestIdIfAbsent())
                .uri(uri)
                .requestMethod(method)
                .status(ex == null ? "success" : "fail")
                .duration(System.currentTimeMillis() - start)
                .requestHeader(requestheader)
                .responseBody(responseBody)
                .responseHeader(responseHeader)
                .stackTrace(ex == null ? "" : ExceptionUtils.getFullStackTrace(ex))
                .build();
        if ("POST".equals(method)) {
            content.setTargetSystem(siteName);
        } else {
            content.setTargetSystem(targetHost);
        }
        if (!Objects.isNull(responseStatus)) {
            content.setResponseStatus(responseStatus);
        }
        if ("GET".equals(method) && !Objects.isNull(httpUriRequest) && !Objects.isNull(httpUriRequest.getURI())) {
            content.setRequestUrlParam(httpUriRequest.getURI().getQuery());
        } else {
            HttpPost httpPost = (HttpPost) httpUriRequest;
            HttpEntity entity =  httpPost.getEntity();
            try {
                //entity = new BufferedHttpEntity(entity);
                content.setRequestBodyParam(URLEncodedUtils.parse(entity).toString());

            } catch (IOException e) {
                LogUtils.error(e, log, "io转换错误");
                e.printStackTrace();
            }
        }
        return JSONObject.toJSONString(content);
    }
  • 写回答

4条回答 默认 最新

  • 小P聊技术 2021-06-04 10:39
    关注

    29行: responseBody = URLEncodedUtils.parse(entity).toString();

    70行:content.setRequestBodyParam(URLEncodedUtils.parse(entity).toString());


    原因是在同一个httpclient中只能有一个获取entity的方法,你应该把这段数据存储下来,而不是二次获取toString

    评论

报告相同问题?

悬赏问题

  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。
  • ¥15 把VMware项目复制到另一台电脑
  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密