HTTP Basic Authentication With URLConnection

11,617

Basically, here there could be a few things that can go wrong. Does the URL you are trying to connect to exist? If not, may be you may have to redirect. Also, try

connection.getResponseCode();

and make sure it returns 200 or HTTP_OK

Needs more information to say anything. Go to debug mode and try to get the states of different objects used here.

Similar to IOException: "Received authentication challenge is null" (Apache Harmony/Android)

Share:
11,617
CodeDecode
Author by

CodeDecode

Updated on June 04, 2022

Comments

  • CodeDecode
    CodeDecode almost 2 years
    connection = (HttpURLConnection)new URL(mURL).openConnection();
    connection.setRequestMethod("GET");
    String basicAuth = params[0] + ":" + params[1];
    basicAuth = "Basic " + new String(Base64.encode(basicAuth.getBytes(),Base64.NO_WRAP));
    connection.setRequestProperty("Authorization",basicAuth);
    connection.connect();
    InputStream in = connection.getInputStream();
    

    I have gone through all possible questions based on this on SO but with out any success. I am trying to connect to my server using basic authentication. But when I try to access the input stream from URLConnection it throws IOException. Thanks for any help from any one of you.