0

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.

nilkash
  • 7,408
  • 32
  • 99
  • 176
  • What device do you use, I had this code working on Nexus 7 and emulator, but doesn't work on Samsung P7510... just throw another exception when I'm trying to getResponseCode() second time :( – Happy Dev Oct 21 '13 at 01:12
  • @Happydev thank you for your help. I solve this problem by adding some authentication header on server side and its working thank you for help. – nilkash Oct 21 '13 at 03:59

1 Answers1

0

Take a look at this question: IOException: Received authentication challenge is null.

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.

Community
  • 1
  • 1
Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • thank you for valuable information. I will check you answered and let you know about this problem – nilkash Sep 28 '13 at 12:07