简体   繁体   English

如何在Java Applet中进行网站身份验证?

[英]How to do Website Authentication in Java Applet?

I have a wireless IP camera and I want to make my own web page to show its live stream. 我有一个无线IP摄像机,我想创建自己的网页以显示其实时流。 The address of the stream is "http://192.168.1.2:8082/index.cgi" (supposed) and it requires User authentication. 流的地址是"http://192.168.1.2:8082/index.cgi" (假定),它需要用户验证。 This means that when we enter the above URL in browser it asks for username and password. 这意味着,当我们在浏览器中输入上述URL时,它会要求输入用户名和密码。

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. 我想要的是制作一个Java小程序,在加载Java小程序时,它应该验证URL并显示图像/流。

This is the situation, now the basic problem is that 这是情况,现在的基本问题是

Q: How to do Http authentication in Java applet? 问:如何在Java applet中进行Http身份验证?

I shall be thankful for every answer. 我会很感激每一个答案。

You can achieve it by making an HttpURLConnection and appending username and password with the URL. 您可以通过建立HttpURLConnection并将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: 另外,您也可以尝试(不确定在Applet中是否允许)为HTTP请求设置默认的身份验证器(而不是在URL中附加用户名/密码):

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. 您还可以使用非常易于使用的Apache HttpComponents HttpClient To know more about how HTTP requests work in Java, see this answer . 要了解有关HTTP请求在Java中如何工作的更多信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM