我是一個HttpsURLConnection的問題,我似乎無法解決。基本上,我向服務器發送了一些信息,如果某些數據錯誤,服務器會向我發送一個500響應代碼。但是,它也會在響應中發送一條消息,告訴我哪一位數據是錯誤的。問題在於,當我讀入消息時,消息總是空的。我認爲這是因爲在讀取流之前總是會拋出一個filenotfound異常。我對嗎?我也嘗試閱讀errorstream,但這總是空的。這裏有一個片段:HttpsURLConnection連接問題
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();
}
}