3

I have to access an https site through my java code. But it returns 401 response. I included my code below.

try {        
 URL u = new URL(url);
 HttpURLConnection http = (HttpURLConnection)u.openConnection();
 http.setAllowUserInteraction(true);
 http.connect();

 String userpassword = "HP:M0lveau";
 byte[] encoded = Base64.encodeBase64(userpassword.getBytes());
 String encodedAuthorization = new String(encoded);

 http.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
 InputStream is = http.getInputStream();

 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 StringBuilder stringBuilder = new StringBuilder();
 String line = null;
 while ((line = reader.readLine()) != null) {
   stringBuilder.append(line + "\n");
 }
 return stringBuilder.toString();

} catch (IOException ioe) {
 logger.debug("fetchDataFromServer:IOException");
 return null;
}

Please help as early as possible.. Thanks in advance....

skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116

3 Answers3

2

401 response means that the request requires user authentication. LOOK here for help

Have something like

public static String userNamePasswordBase64(String username, String password)
{
    return "Basic " + base64Encode (username + ":" + password);
}

static public String base64Encode(String s)
{
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Base64OutputStream out = new Base64OutputStream(bout);
    try
    {
        out.write(s.getBytes());
        out.flush();
    } catch (IOException e)
    {
    }
return bout.toString();
}

and then use

http.setRequestProperty ("Authorization",userNamePasswordBase64("HP","M01veau"));
http.connect();

Also check manually that these credentials are working fine on the given url. Also See this great tutorial

Community
  • 1
  • 1
ayush
  • 14,350
  • 11
  • 53
  • 100
  • I think you meant setting username and password. But I have set it. – skmaran.nr.iras Jan 21 '11 at 05:31
  • String userpassword = "HP:M0lveau"; byte[] encoded = Base64.encodeBase64(userpassword.getBytes()); String encodedAuthorization = new String(encoded); http.setRequestProperty("Authorization", "Basic " + encodedAuthorization); – skmaran.nr.iras Jan 21 '11 at 05:31
  • Thanks for your help..I tried it. But couldnt. I also used HttpsURLConnection. Is any other mistakes in my code? – skmaran.nr.iras Jan 21 '11 at 06:48
  • 2
    @Sarika: did your credentials(usename/passwd) work when u accessed this site normally through your browser?? what is the url? – ayush Jan 21 '11 at 08:37
  • 1
    @Sarika : see the new function base64Encode() also in edited answer. Maybe it will help.see this also http://www.heatonresearch.com/articles/146/page5.html – ayush Jan 21 '11 at 08:45
  • May I know the package for Base64OutputStream? – skmaran.nr.iras Jan 21 '11 at 09:56
  • 1
    @sARIKA : As Maurice mentioned "Try to set the Authorization header before the call to connect()" like i have in my answer. The package is package gnu.inet.mime.base64 . check this - http://www.koders.com/java/fid2C8F058DD15AB1913D9D427DE5C10853CD711FB7.aspx?s=base64#L2 and this http://www.mozilla.org/projects/security/pki/jss/javadoc/org/mozilla/jss/util/Base64OutputStream.html – ayush Jan 22 '11 at 21:11
  • @ayush : I think the problem is not with the https. Because I have got response for the host site. So the problem may be with the authentication part. Am I correct? – skmaran.nr.iras Jan 24 '11 at 09:25
  • 1
    @Sarika : what do u mean by "response for host site"? how and what response did you get? The authentication code i gave above is correct assuming your username and password are correct.and very imp - ARE YOUR USERNAME/PASSWORD WORKING FINE WHEN YOU ACCESS THIS URL NORMALLY THROUGH A BROWSER?? – ayush Jan 24 '11 at 11:00
  • Sorry! I meant this - "https://www.cluemaster.com". The response is its page source(html). MY USERNAME AND PASSWORD WORKING FINE WHEN I ACCESS THIS URL NORMALLY THROUGH A BROWSER. I tried with your code. But coudnt complete it since I didnt know how to include package gnu.inet.mime.base64 in my package. – skmaran.nr.iras Jan 24 '11 at 12:02
  • @ayush : I got it. I have added the code below.. Thanks a lot for your helping mind. – skmaran.nr.iras Jan 24 '11 at 12:54
2

Try to set the Authorization header before the call to connect()

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
2

I got it.. This is my code.

private static String fetchDataFromServer() throws HttpException, IOException, NoSuchAlgorithmException, KeyManagementException {

        logger.trace("__ENTERING CluemasterData::fetchDataFromServer()");

        try {
            URL u = new URL("https://....");
            HttpsURLConnection http = (HttpsURLConnection)u.openConnection();
            Authenticator.setDefault( new MyAuthenticator() );
            http.setAllowUserInteraction(true);
            http.setRequestMethod("GET");
            http.connect();

            InputStream is = http.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line + "\n");
            }
            return stringBuilder.toString();    
        }
        catch (HttpException he) {

            return null;
        } 
        catch (IOException ioe) {

            return null;
        }

    }

The class MyAuthenticator

import java.net.Authenticator;
import java.net.PasswordAuthentication;

class MyAuthenticator extends Authenticator
   {
   /**
   * Called when password authorization is needed.
   * @return The PasswordAuthentication collected from the
   * user, or null if none is provided.
   */
   protected PasswordAuthentication getPasswordAuthentication()
      {
      return new PasswordAuthentication ( "username", "password".toCharArray() );
      }
   }
skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116