0

I've tried to login into website using Jsoup, but unfortunatelly I've forced with some problems- don't know how should I pass submit button into because there's no id or name for it. Could you take a look how should my code look like?

<form action="http://www.abcde.com/index.php?app=core&amp;module=global&amp;section=login&amp;do=process" method="post" id="login">
<input type="hidden" name="auth_key" value="auth_key">
<input type="hidden" name="referer" value="http://www.abcde.com/">
<h3>Login</h3>

<div class="ipsForm ipsForm_horizontal">
    <fieldset>
        <ul>

            <li class="ipsField ipsField_primary">
                <label for="ips_username" class="ipsField_title">Username</label>
                <div class="ipsField_content">
                    <input id="ips_username" type="text" class="input_text" name="ips_username" size="30" tabindex="0">
                </div>
            </li>
            <li class="ipsField ipsField_primary">
                <label for="ips_password" class="ipsField_title">Password</label>
                <div class="ipsField_content">
                    <input id="ips_password" type="password" class="input_text" name="ips_password" size="30" tabindex="0"><br>
                </div>
            </li>

        </ul>
    </fieldset>

    <div class="ipsForm_submit ipsForm_center">
        <input type="submit" class="ipsButton" value="Login" tabindex="0">
    </div>
</div>
</form>

I've started:

 Connection.Respose loginForm = Jsoup.connect("http://www.abcde.com/").method(Connection.Method.GET)
                        .execute();
    Document document = Jsoup
                        .connect("http://www.abcde.com/)
                        .data("cookieexists", "false")
                        .data("ips_username", "username", "ips_password",
                                "password").cookies(loginForm.cookies()).post();
adolzi
  • 671
  • 2
  • 7
  • 15
  • Take a look here - http://stackoverflow.com/questions/31871801/problems-submitting-a-login-form-with-jsoup You probably don't send all the parameters with your login request. – TDG Apr 23 '16 at 17:46
  • I suppose that, but I have in view source there is no viewstate or eventvalidation. – adolzi Apr 23 '16 at 18:01
  • It was just an example. You'll have to exemine the http traffic between your browser and the server, or that you can add the real URL you're trying to reach. BTW - you have "auth_key" in your source and I guess that you need to use it somewhere. I'd also say that this value changes between sessions. – TDG Apr 23 '16 at 18:23
  • Real url is www.forumowisko.pl - it's polish website – adolzi Apr 23 '16 at 18:40

1 Answers1

0

First you must send a GET request to the server -

Document doc1 = Jsoup.connect("www.forumowisko.pl")
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")
                .get();

Then you extract the auth_key value -

Element e = doc.select("input[id=auth_key]").first();
String authKey = e.attr("value"); 

And now you can send the POST request -

Document doc2 = Jsoup.connect("http://www.forumowisko.pl/index.php?app=core&module=global&section=login&do=process")
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")         
        .data("auth_key", authKey)      
        .data("ips_username", "MyUsername")
        .data("ips_password, "MyPassword")
        .data("rememberMe", "1")
        .data("referer", "http://www.forumowisko.pl/")
        .cookies(doc1.cookies())
        .post();

Notice that the POST request has a different URL from the GET request.

TDG
  • 5,909
  • 3
  • 30
  • 51
  • I've changes userAgent, also doc.select("input[id=auth_key]") to [name=auth_key] and doc1.cookies() to response.cookies() and it works fine. Thank you for help. But I have one extra question, could you just mention how should I properly redirect to other urls and be still logged? Is there any way to send params or every time I have to send whole these params in that way? – adolzi Apr 23 '16 at 19:25
  • I don't understand your question - what are you trying to do after loging in? – TDG Apr 23 '16 at 19:34
  • I meant sending cookies, but I solved it yet. Thanks once more – adolzi Apr 23 '16 at 20:06