0

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.

Azeem Akram
  • 223
  • 6
  • 9
  • 21

1 Answers1

1

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.

Community
  • 1
  • 1
Umer Hayat
  • 1,913
  • 4
  • 30
  • 56
  • :: my applet get starts but the authentication gives this error i am quoting the full error below "Exception : java.lang.RuntimeException: java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.1.20:8082 connect,resolve)" – Azeem Akram Jun 12 '12 at 06:03
  • @azeemAkram Which solution are you using? are you appending username/password in url ? or using authenticator? – Umer Hayat Jun 12 '12 at 07:01
  • the first one, appending udername/passowrd in url. – Azeem Akram Jun 12 '12 at 09:54
  • @azeemAkram: hmm .. looks like it has something to do with security. Please have a look at my answer here : http://stackoverflow.com/a/10992811/945945. – Umer Hayat Jun 12 '12 at 10:19