-1

Hi in my android application I am using HttpUrlconnection for web services. So for any web resource request with response 401 it's not giving any header fields or response code.And it showing content length -1.It Fires exception java.io.IOException: Received authentication challenge is null. Now my problem is that how to check response code for such conditions. Need Help. I check those request in rest clients and there I found that my response coming properly with header response code with 401 and other fields also

My request looks like

String urlString ="http://WWW.ABC.com";
URL url = new URL(urlString);
login_urlConnection = (HttpURLConnection) url.openConnection();
login_urlConnection.setRequestMethod("GET");
int statusCode = login_urlConnection.getResponseCode() // Here I am getting exception I want this status code to check authentication failure 

How to solve this problem. Need Help. Thank you.

I did same call with HttpClient and then I am able to get response code then why not with HttpUrlConnection. Am I doing something wrong Need Help.

nilkash
  • 7,408
  • 32
  • 99
  • 176

2 Answers2

0
   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     read(in);
    finally {
     urlConnection.disconnect();
   }

for reading use this block:

int i;
      char c;

      try{
         // new input stream created
         is = new FileInputStream("C://test.txt");

         System.out.println("Characters printed:");

         // reads till the end of the stream
         while((i=is.read())!=-1)
         {
            // converts integer to character
            c=(char)i;

            // prints character
            System.out.print(c);
         }
      }catch(Exception e){

         // if any I/O error occurs
         e.printStackTrace();
      }finally{

         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
Lebedevsd
  • 950
  • 5
  • 12
  • My code is working fine for other requests where no authentication failure occurred. Mean I am not able to get repose code and header for those requests where authentication failure occurred. – nilkash Sep 30 '13 at 10:24
0

urlString should be changed into "http://WWW.ABC.com". Url Scheme (like http:// or ftp://, and so on) is needed.

Also, you need to make connection to the server.

login_urlConnection.setRequestMethod("GET");
try {
    login_urlConnection.connect();
} catch (IOException e) {
    e.printstackTrace();
}
int statusCode = login_urlConnection.getResponseCode()
wktk
  • 147
  • 2
  • 6
  • Thank you for response. I tried you are solution. But still it is giving same problem. Is there Any other solution. It's giving proper status code for 200 or any other status code just when authentication failure occurred it givinf header length with -1 and no status code. – nilkash Sep 30 '13 at 10:21