源代码如下,爆: 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);
    }

 

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

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


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

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

同一个httpclient中只能有一个获取entity的方法

httpResponse对象的值是哪里来的,有没有报错具体的行号信息。