【发布时间】:2015-03-11 05:09:55
【问题描述】:
我正在使用这个功能 -
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 错误, 但是,如果我评论 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();
}
【问题讨论】:
-
我也尝试在 finally 块中关闭连接,但不工作,同样的事情发生。
-
stackoverflow.com/questions/9150200/… 这个链接应该有帮助。
-
@Sarah 我已经准备好最终关闭,但没有工作。请阅读我的第一条评论。
-
您能否提供一个示例,说明您从何处调用此
GetResponse()方法?
标签: java io stream httpurlconnection httpsurlconnection