I am using this function -
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{
}
}
If I am using this -
con.disconnect();
Then I am getting java.io.IOException: stream is closed error, But if I comment the con.disconnect() line then everything is working fine. I don't know why this is happening.
Calling function
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();
}