This discussion is archived
4 Replies Latest reply: Mar 18, 2013 4:21 PM by EJP RSS

Java.net.Authenticator gives 401 errors.

888525 Newbie
Currently Being Moderated
I am using Authenticator to connect to a URL but I get error

401:Unauthorised for URL http://mymachine/ReportServer/Pages/Myreport

public class ReportAuthenticator extends Authenticator {


private String username = "mydomain\\username";
private String password = "password";

public PasswordAuthentication getPasswordAuthentication()
{
System.out.println("The username and password are"+ username);
System.out.println("the password is"+password.toCharArray());
return new PasswordAuthentication(username,(password.toCharArray()));
}

}


And My servlet which is using the authenticator is



public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

Authenticator.setDefault(new ReportAuthenticator());
String urlString = "http://mymachine/ReportServer/Pages/Myreport";
System.out.println("The url string is"+urlString);
URL url = new URL(urlString);
HttpURLConnection repCon = (HttpURLConnection)url.openConnection();
repCon.setRequestMethod("POST");
repCon.setDoOutput(true);
repCon.setUseCaches(false);
repCon.setFollowRedirects(false);
repCon.setRequestProperty("User-Agent", "Mozilla/5.0");
repCon.setRequestProperty("Content-type", "application/pdf" );
repCon.setRequestProperty("Content-length", Integer.toString(parameterString.length()));
repCon.setRequestProperty("Authorization","Basic");
System.out.println("the response code"+repCon.getResponseCode());
System.out.println("the content from url connection "+repCon.getContent());
PrintWriter repOutStream = new PrintWriter(repCon.getOutputStream());
repOutStream.println(parameterString);
repOutStream.close();
InputStream repInStream = reportCon.getInputStream();
}

When I run this the sysyemout given in ReportAuthenticator do not show in the output instaed I get 401 error at line
System.out.println("the response code"+repCon.getResponseCode());
It says unauthorised for the url.
Does that mean the authenticator is not being called?
Is there any other authentication configuration missing on the server.
The server from where my Java application is deployed is different from the server in the url from which i am trying to connect.

Please help

Thanks,
  • 1. Re: Java.net.Authenticator gives 401 errors.
    gimbal2 Guru
    Currently Being Moderated
    885522 wrote:
    It says unauthorised for the url.
    Does that mean the authenticator is not being called?
    Or the credentials are in fact wrong. I'm rusty, but I believe the password needs to be Base64 encoded.
    Is there any other authentication configuration missing on the server.
    The server seems to be doing its job - keeping you out. So the authentication configuration must be present.
  • 2. Re: Java.net.Authenticator gives 401 errors.
    888525 Newbie
    Currently Being Moderated
    Thanks so much for the reply.
    The credentials are correct.If I paste the same url in my IE .I am prompted for userid and password and I give the same credentials
    it works fine and the url opens up.
    Its only when i try to connect from Java it is giving unauthorised error 401.

    Strangely I dont know why the systemouts in ReportAuthenticator are now showing up?
    How can I try to have password as base64 authenticated.I have tried changing


    repCon.setRequestProperty("Authorization","NTLM");

    Still the same error looks like the authenticator class is not working .
    I have been struggling with this for a couple of days now and still no solution Please help

    Thanks,
  • 3. Re: Java.net.Authenticator gives 401 errors.
    888525 Newbie
    Currently Being Moderated
    Can you please help with this? I am stuckup with this for days.

    Thanks
  • 4. Re: Java.net.Authenticator gives 401 errors.
    EJP Guru
    Currently Being Moderated
    repCon.setRequestMethod("POST");
    repCon.setDoOutput(true);
    "POST" is the default when you call doOutput(true). Remove that.
    repCon.setRequestProperty("Content-type", "application/pdf" );
    You are sending a PDF? Doesn't seem likey, as you have it in a String. Remove that.
    repCon.setRequestProperty("Content-length", Integer.toString(parameterString.length()));
    Don't set that. Java will do it, and do it accurately. Remove that.
    repCon.setRequestProperty("Authorization","Basic");
    The Authenticator will do this when necessary. Remove that. At present you are saying there is authorization with the request even when there isn't (i.e. the first time, before the server asks for it).
    Does that mean the authenticator is not being called?
    You can test that for yourself.
    Is there any other authentication configuration missing on the server.
    You've already overdone it. Try the changes mentioned above.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points