Amjad Aziz Amjad Aziz - 1 month ago 7
Java Question

Basic Authentication failed in Windows server 2012 Using Java

I am trying to connection my certificate service over windows server 2012. I have enabled Basic Authentication over server and I can access my service from browser providing user name and password.
I am opening connection using

HttpURLConnection
in Java.
Connection is successful using following code.

static class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("trying to authenticate");
return new PasswordAuthentication("user", "password".toCharArray());
}
}

Authenticator.setDefault(new MyAuthenticator())


But I need to connect using following code and it gives me 401 error:

String encoding = Base64.encode("user:password".getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);


Any idea why I am getting 401.

Thanks,

Answer

Basic authentication for Default Web Site on IIS Manager was enabled but Basic Authentication for specific site was not enabled. By enabling Basic Authentication for specific Website resolved the issue.

Comments