java response 401,获取响应头时,401发生

Hi I am working on android and java.So my problem is that whenever 401 occurred I am not able to get response header and also not able to get status code. I am using http url connection.

My code looks like this:

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();

}

I am not getting any field from response header. I know this is authentication problem and my server giving 401 in response but I am not able to get that response code. Am I doing anything wrong. How to handle this kind of exception. Need Help Thank you.

解决方案

It seems that the exception is thrown because the response doesn't include a WWW-Authenticate header (See the spec). I can't be sure this is your case, however you can check it, and maybe, if that's the case and you are in control of the server party, can try to fix the server code to return the header.

Otherwise, you can only catch the IOException and report it to your application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值