通过Jsoup发布方法登录网站不起作用 [英] Login to website through Jsoup post method not working

查看:50
本文介绍了通过Jsoup发布方法登录网站不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,可用于以编程方式登录网站.但是,它不返回登录页面的html(带有用户数据信息),而是返回登录页面的html.我试图多次找出问题所在,但似乎找不到.

I have the following code which I am using to login to a website programmatically. However, instead of returning the logged in page's html (with user data info), it returns the html for the login page. I have tried to find what's going wrong multiple times but I can't seem to find it.

 public class LauncherClass {

static String username = "----username here------"; //blocked out here for obvious reasons
static String password = "----password here------";
static String loginUrl = "https://parents.mtsd.k12.nj.us/genesis/parents/j_security_check";
static String userDataUrl = "https://parents.mtsd.k12.nj.us/genesis/parents?module=gradebook";

public static void main(String[] args) throws IOException{

LauncherClass launcher = new LauncherClass();
launcher.Login(loginUrl, username, password);

}

public void Login(String url, String username, String password) throws IOException {

    Connection.Response res = Jsoup
            .connect(url)
            .data("j_username",username,"j_password",password)
            .followRedirects(true)
            .ignoreHttpErrors(true)
            .method(Method.POST)
            .userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36")
            .timeout(500)
            .execute();

    Map <String,String> cookies = res.cookies();

    Document loggedIn = Jsoup.connect(userDataUrl)
            .cookies(cookies)
            .get();

    System.out.print(loggedIn);

    }
}

[注意]登录表单上有一行:

[NOTE] The login form does have a line:

 <input type="submit" class="saveButton" value="Login">

但这没有名称"属性,因此我没有发布

but this does not have a "name" attribute so I did not post it

任何答案/评论都值得赞赏!

Any answers/comments are appreciated!

[UPDATE2]对于登录页面,浏览器显示以下内容...

[UPDATE2] For the login page, browser displays the following...

 ---General
    Remote Address:107.0.42.212:443
    Request URL:https://parents.mtsd.k12.nj.us/genesis/j_security_check
    Request Method:POST
    Status Code:302 Found
----Response Headers
    view source
    Content-Length:0
    Date:Sun, 26 Jul 2015 20:06:15 GMT
    Location:https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true
    Server:Apache-Coyote/1.1
----Request Headers
    view source   
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    Cache-Control:max-age=0
    Connection:keep-alive
    Content-Length:51
    Content-Type:application/x-www-form-urlencoded
    Cookie:JSESSIONID=33C445158EB6CCAFFF77D2873FD66BC0;         lastvisit=458D80553DC34ADD8DB232B5A8FC99CA
    Host:parents.mtsd.k12.nj.us
    HTTPS:1
    Origin:https://parents.mtsd.k12.nj.us
    Referer:https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4)                 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36
----Form Data
    j_username: ---username here---
    j_password: ---password here---        

推荐答案

您必须分两个阶段登录该网站.
阶段1 - 您向此URL发送GET请求-https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true,并获得session cookies.
第二阶段-
您发送带有用户名和密码的post请求,并添加在阶段1中获得的cookies.
的代码是-

You have to login to the site in two stages.
STAGE 1 - You send a GET request to this URL - https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true and you get the session cookies.
STAGE 2 -
You send a post request with your username and password, and add the cookies you got on stage 1.
The code for that is -

Connection.Response res = null;
Document doc = null;

try {   //first connection with GET request
        res = Jsoup.connect("https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true")
//                  .userAgent(YourUserAgent)
//                  .header("Accept", WhateverTheSiteSends)
//                  .timeout(Utilities.timeout)
                    .method(Method.GET)
                    .execute();         
    } catch (Exception ex) {
        //Do some exception handling here
    }
try {
        doc = Jsoup.connect("https://parents.mtsd.k12.nj.us/genesis/parents/j_security_check"")
    //          .userAgent(YourUserAgent)
    //          .referrer(Referer)
    //          .header("Content-Type", ...)
                .cookies(res.cookies())
                .data("j_username",username)
                .data("j_password",password)                    
                .post();
    } catch (Exception ex) {
        //Do some exception handling here
    }
    //Now you can use doc!

您可能必须为两个请求添加不同的HEADERS,例如userAgentreferrercontent-type等.在第二个请求的末尾,doc应该具有站点的HTML.

You may have to add for both requests different HEADERS such as userAgent, referrer, content-type and so on. At the end of the second request, doc should have the HTML of the site.

无法登录该站点的原因是,您发送的post请求没有session cookies,因此这是来自服务器的无效请求.

The reason that you cannot login to the site is that you are sending the post request without the session cookies, so it's an invalid request from the server.

这篇关于通过Jsoup发布方法登录网站不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆