当前位置: 首页 > 面试题库 >

通过Jsoup发布方法登录网站不起作用

姬衡
2023-03-14
问题内容

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

 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);

    }
}

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

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

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

任何答案/评论表示赞赏!

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

 ---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阶段-您GET向该URL 发送请求-
https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true并获得session cookies
第2阶段-
您发送post带有用户名和密码的请求,然后添加cookies您进入第1阶段的请求。
该代码为-


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该站点的。

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



 类似资料:
  • 问题内容: 使用Jsoup登录网站需要什么?我相信我的代码是正确的,但是我从未使用Jsoup成功登录到网站,因此我可能会丢失一些东西。这是我的代码: 返回一个显示未成功登录的登录页面。有一个名为的输入值,我认为这可能是为什么它不起作用的原因。这个值会随着时间的推移而上升。我运行了两次代码,时间变量返回了和。我的代码需要大约10秒钟的时间来打印文档,因此,变量在发送发布请求时是否已经更改?我不确定这

  • 我正在尝试使用jsoup登录一个网站,我很确定我正在解析所有需要解析的东西,我只是不知道出了什么问题。 我用这个做参考:http://cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf 以下是我的AsycntTask doInBackground中的代码: 但问题是,当我登录时,它不包含页面的文档,它包含一个错误页面的文档,该页面只显示

  • 问题内容: 在此网站上,您可以输入您的学生卡号,然后显示该卡上还剩多少钱。我想使用JSOUP获得信息。这是我当前的代码,但是不起作用, 我没有太多经验,所以我不知道在哪里寻找问题。一些想法: 我应该使用还是? 在使用chrome devoloper工具时,发布数据是我使用功能发送的所有数据。但是,如果发送时出现错误,为什么? 我应该发送解密或加密的数据吗?(两者均在chrome devoloper

  • 我试图登录使用领英J汤,我已经尝试了几种方法,但总是遇到错误403,我哪里出错了? 测试一 错误控制台 组织。jsoup。HttpStatusException:获取URL时发生HTTP错误。状态=403,URL=https://www.linkedin.com在org。jsoup。帮手HttpConnection$响应。在org上执行(HttpConnection.java:590)。jsoup

  • 问题内容: 我尝试使用我从已经发布的问题的答案中读取的这些行登录我的Facebook帐户,但无论如何我都无法登录!我正在寻找一些更正代码的提示: PS:不,我不想使用Facebook API! 问题答案: 在请求中传递了许多其他参数: 并且不要忘记参数。Facebook可能会为登录请求提供某种一次性令牌,以防止绕过Facebook API。

  • 问题内容: 我已经广泛阅读了有关此操作的信息,并尝试了许多不同的版本,但无法使其正常工作。 基本上,我只想登录ConEdison网站并抓取我的帐单记录。这是我所拥有的: 我知道该信息是正确的(尽管我不知道是否真的需要传递没有值的数据参数)。 我没有任何错误,只是打印了登录页面(https://apps.coned.com/cemyaccount/NonMemberPages/Login.aspx?