获取响应头时,401发生 [英] Get Response header when 401 occurred

查看:1253
本文介绍了获取响应头时,401发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在Android和java.So我的问题是,每当发生401我无法得到响应头,也没能获得状态code。我使用HTTP URL连接。 我的code是这样的:

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

我没有收到任何领域,从响应头。我知道这是认证问题,我的服务器给予响应401,但我不能让这种反应code。难道我做错什么。如何处理这种类型的异常。需要帮助谢谢你。

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.

推荐答案

看看这个问题: IOException异常:收到认证挑战是空

看来,异常被抛出,因为响应不包括 WWW验证头(见的规范)。我不能肯定这是你的情况,但是你可以检查一下,也许,如果是这样的话,你在服务器方的控制权,可以尝试修复服务器code返回头。

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.

否则,你只能赶上IOException异常,并报告给你的应用程序。

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

这篇关于获取响应头时,401发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆