Reputation: 233
I have a wireless IP camera and I want to make my own web page to show its live stream.
The address of the stream is "http://192.168.1.2:8082/index.cgi"
(supposed) and it requires User authentication. This means that when we enter the above URL in browser it asks for username and password.
All what I want is that to make a java applet, on loading the java applet, it should authenticate the URL and show the image/stream.
This is the situation, now the basic problem is that
Q: How to do Http authentication in Java applet?
I shall be thankful for every answer.
Upvotes: 0
Views: 812
Reputation: 2001
You can achieve it by making an HttpURLConnection and appending username and password with the URL. As:
URL myURL = new URL("http://192.168.1.2:8082/index.cgi?username=user&password=");
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
myConnection.setDoOutput(false);
int status = ((HttpURLConnection) myConnection).getResponseCode();
Alternatively (instead of appending username/password to url), you can try( not sure if it is allowed in Applet) setting the default authenticator for http requests like this:
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ("username", "password".toCharArray());
}
});
You can also use Apache HttpComponents HttpClient which is very easy to use. To know more about how HTTP requests work in Java, see this answer.
Upvotes: 1