QQ_hoverer 2021-06-07 11:35 采纳率: 0%
浏览 66

抛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();
                    System.out.println(responseBody);
                } 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());
                System.out.println(content.getRequestBodyParam());
            } catch (IOException e) {
                LogUtils.error(e, log, "io转换错误");
                e.printStackTrace();
            }
        }
        return JSONObject.toJSONString(content);
    }
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 17:53
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥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解密