• Post Reply Bookmark Topic Watch Topic
  • New Topic

Open password protected URL  RSS feed

 
pinky suresh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pinky,
If you UseCodeTag then it is very easy to read code for you and others.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.

Error 500 has nothing to do with authentication - it is an internal server error (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). If your authentication would have failed you would have gotten a 401 (unauthorized) or 403 (forbidden) error. Can you open the URL from a browser without problems?
 
pinky suresh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes.. when i enter the url in browser it prompts me for username and password which after entering will take me to the home page.

My requirement is that , the url should not promt me for username and password instead take the details from backend and take me directly to home page.

Please advise how this can be achieved. Also let me know if I am missing any thing.

Kindly let me know if this is feasible using Ajax.If yes, kindly provide me the steps to achieve the same.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, do you want to do this using AJAX in a browser, or using Java code? If the latter, why are you setting the method as HEAD instead of GET?
If the former, then the XMLHttpRequest object lets you set request headers in a similar way to what you've done in the Java code, although you'd need to find a JavaScript implementation of base-64.
 
pinky suresh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you please brief me on achieving this using java.
Also kindly let me know the mistake that I am doing.
After setting the authenticator with username and password, how to set that authenticator object to url which I am trying to open connection.
I could not find setAuthenticatoe(Authenticator) for urlConnection.

Please help.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the code you posted work if you replace HEAD by GET?

Here's example code for that: http://www.exampledepot.com/egs/java.net/Auth.html
 
pinky suresh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Even if I replace HEAD with GET the code does not work.
Please suggest me to feed username and password to server hence bypassing username password prompt while accessing a URL.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this "HttpURLConnection.setFollowRedirects(false); " to set true.
 
pinky suresh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use HttpURLConnection.setFollowRedirects(true) i am getting 403 error.
Please help me to fix this.
Thanks in advance.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!