HttpsURLConnection连接问题

ste*_*mcb 5 java android httpurlconnection

我有一个HttpsURLConnection的问题,我似乎无法解决.基本上,我正在向服务器发送一些信息,如果其中一些数据错误,服务器会向我发送500响应代码.但是,它也会在响应中发送一条消息,告诉我哪个数据位错误.问题是当我读取它时消息总是空的.我认为这是因为在读取流之前总会抛出一个filenotfound异常.我对吗?我也试过读错误流,但这总是空的.这是一个片段:

    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();
    }
    }
Run Code Online (Sandbox Code Playgroud)

ste*_*mcb 3

因此,为了跟进这个问题,我是如何解决这个问题的:

public static String getResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {
        if(conn.getResponseCode()>=400){
            is = conn.getErrorStream();
        }
        else{
            is=conn.getInputStream();
        }
        ...read stream...
}
Run Code Online (Sandbox Code Playgroud)

似乎像这样调用它们会产生带有消息的错误流。感谢您的建议!