流因关闭连接而关闭错误



我正在使用这个函数-

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:流是关闭错误,但是如果我评论 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();
}

来自 JavaDoc of HttpURLConnection(HttpsURLConnection 扩展):

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

在你的GetResponse()方法中,你得到了一个引用HttpsURLConnectionInputStream作为BufferedReader。 但是,当您使用 con.disconnect() 时,您关闭了该基础InputStream

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

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

这是一种方法:

GetResponse():

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;
}

电话代码:

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
}

相关内容

  • 没有找到相关文章

最新更新