简体   繁体   English

Java上的HTML身份验证不起作用

[英]HTML Authentication on Java doesn't work

I am trying to send a website my username or password using some jar file from Apache and trying to read everything from the website with the help of my method "loadpage". 我正在尝试使用Apache的一些jar文件向网站发送我的用户名或密码,并尝试在我的方法“loadpage”的帮助下从网站上读取所有内容。

It doesn't work. 它不起作用。 After I execute my method "loadpage". 我执行我的方法“loadpage”后。 I still get the Streams from the main page, which I don't need to be logged in. I want to have Streams after i am logged in. 我仍然从主页面获取Streams,我不需要登录。我希望在登录后拥有Streams。

  public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("https://justawebsite", 8080),
                    new UsernamePasswordCredentials("username","password"));

            HttpGet httpget = new HttpGet("https://justawebsite");

            System.out.println("executing request" + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }

I tried it also without the help of some jarfile from Apache. 我没有得到Apache的一些jarfile的帮助也尝试了它。

public  void LogIn(String url1) throws Exception{
        URL url = new URL(url1);

        String userPassword = "username"+":"+"password";
        String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
        //con.setRequestProperty("Cookie", "JSESSIONID="+getSid());

        URLConnection con = url.openConnection();
        //con.connect();
        con.setRequestProperty("Cookie", "JSESSIONID=" + encoding);


        con.connect();

    }

My Method Loadpage: it works because I tried it with some other websites, which don't need an authentication. 我的方法加载页面:它的工作原理是因为我尝试了其他一些不需要身份验证的网站。

public String loadPage(String url) throws Exception { public String loadPage(String url)throws Exception {

    URLConnection con = new URL(url).openConnection();
    StringBuilder buffer = new StringBuilder();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String line;
    while((line=in.readLine())!= null){
        buffer.append(line);
    }
    in.close();
    return buffer.toString();

}

You should first check which authentication type is the website using as pointed out by Behe, as there are different Web Authentication Types . 您应首先检查Behe指出的网站使用的身份验证类型 ,因为有不同的Web身份验证类型

Your first example is using Basic Authentication, but the website is probably expecting some kind of Form Based Authentication. 您的第一个示例是使用基本身份验证,但该网站可能期望某种基于表单的身份验证。

If latest is your case, you should check this Client HTTP Programming Primer which includes 如果您的情况是最新的,则应查看此客户端HTTP编程入门手册 ,其中包括

  • enter username and password in a web form and hit the "login" button 在Web表单中输入用户名和密码,然后单击“登录”按钮

Actuualy what i did was correct. Actuualy我所做的是正确的。 I found the Solution just with luck.I don't know why, but i had to write in.readLine(); 我发现解决方案只是运气。我不知道为什么,但我必须写in.readLine(); one time before the 一次之前

in.readLine();

while((line=in.readLine()) != null){
buffer.append(line);
}
in.close()

to get the Streams after i was logged in. If i dont do this line before While.. i get the Streams before i was logged in, allthough i was logged in. 在我登录后获取Streams。如果我之前没有这样做,我会在登录之前得到Streams,尽管我已经登录了。

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

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