我有一个问题,涉及到HttpsURLConnection,但是我似乎无法解决它。基本上,我向服务器发送一些信息,如果其中某些数据不正确,服务器将向我发送500响应代码。然而,它还会在响应中发送一条消息,告诉我哪个数据有误。问题在于,当我读取该消息时,它总是为空的。我认为这是因为在流被读取之前,总是会抛出filenotfound异常。我的想法对吗?我尝试读取errorstream,但它总是为空的。以下是代码片段:
conn = (HttpsURLConnection) connectURL.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",
Integer.toString(outString.getBytes().length));
DataOutputStream wr = new DataOutputStream(conn
.getOutputStream());
wr.write(outString.getBytes());
wr.flush();
wr.close();
if(conn.getResponseCode>400{
String response = getErrorResponse(conn);
public String getErrorResponse(HttpsURLConnection conn) {
Log.i(TAG, "in getResponse");
InputStream is = null;
try {
//is = conn.getInputStream();
is = conn.getErrorStream();
// scoop up the reply from the server
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char) ch);
}
//System.out.println(sb.toString());
return sb.toString();
// return conferenceId;
}
catch (Exception e){
e.printStackTrace();
}
}