通过关闭连接关闭流错误

时间:2015-03-11 05:09:55

标签: 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
        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();
}

1 个答案:

答案 0 :(得分:2)

来自JavaDoc of HttpURLConnection(HttpsURLConnection扩展):

  

如果此时持久连接空闲,则调用disconnect()方法可能会关闭底层套接字。

GetResponse()方法中,您引用HttpsURLConnection InputStream作为BufferedReader。但是,当您使用con.disconnect()时,您关闭了基础InputStream

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

在完成con.disconnect()之前,您需要重新安排代码,不要致电con.disconnect()

这是一种方法:

的GetResponse():

BufferedReader

致电代码:

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

        // Send post request
        con.setDoOutput(true);
        wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
    }
    catch(Exception e) { //better to catch more specific than Exception
        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{
        if(wr != null) {
            wr.close();
        }
    }
    return con;
}