Using Jsoup to POST login data

后端 未结 2 1159
死守一世寂寞
死守一世寂寞 2021-01-19 16:12

I\'m trying to log into this website: http://deeproute.com

This is my code.

            Connection.Response res = null;
            Connection homeCo         


        
相关标签:
2条回答
  • 2021-01-19 16:52

    You need to read form before posting! You are missing param subbera=Login.


    public static void main(String[] args) throws Exception {
    
        Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
                .method(Connection.Method.GET)
                .execute();
    
        Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
                .data("cookieexists", "false")
                .data("name", "username")
                .data("password", "pass")
                .data("subbera", "Login")
                .cookies(loginForm.cookies())
                .post();
    
    }
    
    0 讨论(0)
  • 2021-01-19 17:01

    Might be a few(many actually) months too late but i found that i had to do some web scrapping where the site was designed really confusing (the site i needed to extract data from). Also had to login first to get cookie info. @MariuszS's answer was helpful but the only problem was, i wasn't sure what the expected Map/Key-Value-Pairs were supposed to be. Thanks to Javascript, i quickly retrieved the form keys and values and was able to login successfully. Here's the Javascript:

    var str = "";
    
    //the form selector i.e.
    //that which is to be submitted to. In
    //my case, i needed to submit the entire body
    var arr = $("input[name]");
    
    for(var i = 0, l = arr.length; i<l; i++){
    str+= '.data("'+ arr[i].name +'", "'+ arr[i].value +'")'
    }
    

    Append the value of "str" to your Jsoup request before

    .method(Connection.Method.GET)
    .execute();
    

    Hope this proves to be somewhat helpful like it was for me :)

    0 讨论(0)
提交回复
热议问题