首页 > 解决方案 > java.io.IOException:流已关闭

问题描述

当我尝试从 http 连接打印响应时,出现以下错误。我想我两次使用流,因此它说流已关闭但不确定。

错误出现在writeInstFile()我尝试使用打印响应的方法上getInputStream().getText()

我只是想创建新的字符串变量并将连接流响应放入该变量并打印它,以便我可以看到当我尝试执行 http 请求时得到的响应:

    private void writeInstFile(File outFile) {
        Properties config = workflowEnvironment.getConfig()        
        //http connection to get Authentication token
        HttpURLConnection connection = connection("https://XXX/RequestToken")

        if (connection.responseCode == HTTP_OK) {
            String authToken = authTokenFromJson(jsonFromDssRequestTokenConnection(connection))
            log.info("Authentication token: $authToken")
            println(connection.getInputStream().getText());
        if (connection.responseCode == HTTP_OK) {
            println("Got http response code: ${connection.responseCode}, message: ${connection.responseMessage}")

            log.info("Successful DSS request to ${connection.getURL()}")

            LazyMap json = jsonFromDssExtractionConnection(connection)
.....
}


private static String authTokenFromJson(LazyMap json) {
        json.value
    }


private static LazyMap jsonFromDssRequestTokenConnection(HttpURLConnection connection) {
        connection.inputStream.withCloseable { inStream ->
            new JsonSlurper().parse(inStream as InputStream)
        } as LazyMap
    }

标签: jsongroovyhttprequestresponserest

解决方案


推荐阅读