Amjad Aziz Amjad Aziz - 1 year ago 91
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

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.setRequestProperty ("Authorization", "Basic " + encoding);

Any idea why I am getting 401.


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download