Hi,
I have a requirement where I have to check if a applicaiton is running by checking the URL of the applicaiton.
I problem is that applicaition URL is password protected.
My requirement is to access that URL by providing username and password from backend through
java program.
I have tried the following..but I am getting 500 as response code instead of 200.
Below is the code that I am trying to run
-----------------------------------------------
public static void main(
String[] args) throws IOException {
String userPassword = "nagsures" + ":" + "Qwer$1234";
URL url = new URL("application_url");
HttpURLConnection.setFollowRedirects(false);
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("HEAD");
con.setRequestProperty ("Authorization", "Basic " + encoding);
int response = con.getResponseCode();
System.out.println("---response is----" + response);
}
_----------------------------------------------------
I have also tried with Authenticator as well.Below is the code for the same
---------------------------------------------------------
public static void main(String[] args) throws IOException
{
final String login ="nagsures";
final String password ="Qwer$1234";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (login, password.toCharArray());
}
});
String url = "application_url";
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
System.out.println("response code is:: " + con.getResponseCode() );
}
---------------------------------------------------------------------------------
Both the trial programs return 500.
Please help me to fix this. Please let me know how to access a password protected url by providing the username and password from backend.