Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭连接导致流关闭错误_Java_Io_Stream_Httpurlconnection_Httpsurlconnection - Fatal编程技术网

Java 关闭连接导致流关闭错误

Java 关闭连接导致流关闭错误,java,io,stream,httpurlconnection,httpsurlconnection,Java,Io,Stream,Httpurlconnection,Httpsurlconnection,我正在使用此函数- public BufferedReader GetResponse(String url, String urlParameters){ HttpsURLConnection con=null; try{ URL obj = new URL(url); con = (HttpsURLConnection) obj.openConnection(); //add reuqest header co

我正在使用此函数-

public BufferedReader GetResponse(String url, String urlParameters){
    HttpsURLConnection con=null;
    try{
        URL obj = new URL(url);
         con = (HttpsURLConnection) obj.openConnection();
        //add reuqest header
        con.setRequestMethod("POST");

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        int responseCode = con.getResponseCode();
        BufferedReader returnValue= new BufferedReader(new InputStreamReader(con.getInputStream()));
        con.disconnect();
        return returnValue;
    }
    catch(Exception e){
        System.out.println("--------------------There is an error in response function ");
        e.printStackTrace();
        LOGGER.error("There is an arror in AjaxCAll function of login controller."+e.getMessage());
        LOGGER.debug("There is an arror in AjaxCAll function of login controller.");
        return null;
    }
    finally{

    }
}
如果我使用这个-

con.disconnect();
然后我得到java.io.IOException:stream is closed error, 但是如果我评论con.disconnect()行,那么一切都正常。我不知道为什么会这样

调用函数

BufferedReader rd = utilities.GetResponse(url, urlParameters);
// Send post request
String line = "";
try{
    while ((line = rd.readLine()) != null) {   //getting error here if i close connection in response method
        // Parse our JSON response
        responsString += line;
        JSONParser j = new JSONParser();
        JSONObject o = (JSONObject) j.parse(line);
        if (o.containsKey("response")) {
            restMessage = (Map) o.get("response");
        } else {
            restMessage = (Map) o;
        }
    }
} finally{
    rd.close();
}
从(HttpsURLConnection扩展的)开始:

如果持久连接在当时处于空闲状态,则调用disconnect()方法可能会关闭底层套接字

GetResponse()
方法中,作为
BufferedReader
获得了对
HttpsURLConnection
InputStream
的引用。但是,当您使用
con.disconnect()
时,您关闭了底层的
InputStream

在调用
GetResponse()
方法的代码中,当您稍后尝试使用返回的
BufferedReader
时,您会得到
java.io.IOException:stream is closed error
,因为您已经使用
con.disconnect()
间接关闭了该流

您需要重新排列代码,以便在完成
BufferedReader
之前不调用
con.disconnect()

这是一种方法:

GetResponse():

呼叫代码:

HttpsURLConnection con = utilities.GetResponse(url, urlParameters);
if(con == null) {
//stop processing or maybe throw an exception depending on what you want to do.
}
BufferedReader rd = null;

// Send post request
String line = "";
try{
    int responseCode = con.getResponseCode();  //what's this for? nothing is being done with the variable
    rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    while ((line = rd.readLine()) != null) {
        // Parse our JSON response
        responsString += line;
        JSONParser j = new JSONParser();
        JSONObject o = (JSONObject) j.parse(line);
        if (o.containsKey("response")) {
            restMessage = (Map) o.get("response");
        }
        else {
            restMessage = (Map) o;
        }
    }
} 
catch(Exception e) { //better to catch more specific than Exception
    //handle exception
}
finally {
    if(rd != null) {
        rd.close();
    }
    con.disconnect(); //we already checked if null above
}

我还尝试关闭了finally block中的连接,但不起作用,同样的事情发生了。这个链接应该会有帮助。@Sarah我已经准备好了,最后尝试了关闭,但没有成功。请阅读我的第一条评论。您能否显示一个调用此
GetResponse()
方法的示例?在这个示例中,
disconnect()
确实关闭了
InputStream
。这并不一定意味着Oracle的JDK代码完全相同(我找不到
HttpsURLConnection
的Oracle源代码),但我希望行为非常相同,以满足中描述的接口。
HttpsURLConnection con = utilities.GetResponse(url, urlParameters);
if(con == null) {
//stop processing or maybe throw an exception depending on what you want to do.
}
BufferedReader rd = null;

// Send post request
String line = "";
try{
    int responseCode = con.getResponseCode();  //what's this for? nothing is being done with the variable
    rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    while ((line = rd.readLine()) != null) {
        // Parse our JSON response
        responsString += line;
        JSONParser j = new JSONParser();
        JSONObject o = (JSONObject) j.parse(line);
        if (o.containsKey("response")) {
            restMessage = (Map) o.get("response");
        }
        else {
            restMessage = (Map) o;
        }
    }
} 
catch(Exception e) { //better to catch more specific than Exception
    //handle exception
}
finally {
    if(rd != null) {
        rd.close();
    }
    con.disconnect(); //we already checked if null above
}