嗨,我正在研究 android 和 java。所以我的问题是,每当发生 401 时,我都无法获取响应标头,也无法获取状态码。我正在使用 http url 连接。我的代码如下所示:
String https_url = "http://abc.com";
HttpsURLConnection con = null;
int status;
URL url;
try {
url = new URL(https_url);
con = (HttpsURLConnection)url.openConnection();
con.setDoInput(true);
int responseCode = con.getResponseCode(); //throw IOException:Received authentication challenge is null
if (responseCode == 200)
{
}
else
{
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
Log.i("&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*", "inside exception");
if (con != null) {
Log.i("&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*", "inside not null");
int responseCodeAfterException = con.getResponseCode();
Log.i("&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*", "inside not null and response");
Log.i("&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*", "inside not null and response"+responseCodeAfterException);
// Handle according to new response code
}
e.printStackTrace();
}
我没有从响应头中获得任何字段。我知道这是身份验证问题,我的服务器给出 401 作为响应,但我无法获得该响应代码。我做错什么了吗。如何处理这种异常。需要帮助 谢谢。