OJunker
OJunker

Reputation: 62

Having trouble loggin into website with Jsoup,

I'm trying to log into a website with jsoup, I'm pretty sure I am parsing all the things I need to, I just can't figure out what's wrong.

I am using this for reference: http://cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf

Here is the code that is in my AsycnTask doInBackground:

            Connection.Response loginForm = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .method(Connection.Method.GET)
                    .timeout(10000)
                    .execute();

            Document doc = loginForm.parse();

            //Random values you need to parse to lectio
            String VIEWSTATEXvalue = doc.select("input[name=__VIEWSTATEX").attr("value");
            String EVENTVALIDATIONvalue = doc.select("input[name=__EVENTVALIDATION").attr("value");

            Log.v("MainActivity",VIEWSTATEXvalue + EVENTVALIDATIONvalue);



            doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .data("m$Content$username2", username)
                    .data("m$Content$password2", password)
                    .data("__VIEWSTATEX",VIEWSTATEXvalue)
                    .data("__EVENTVALIDATION",EVENTVALIDATIONvalue)
                    .data("__EVENTTARGET","m$Content$submitbtn2")

                    .cookies(loginForm.cookies())
                    .post();
            Log.v("MainActivity", doc.toString());

But the problem is it doesn't contain the document of the page when I am logged in, it contains the document for an error page that just says "something went wrong".

I'm guessing this is because I am not parsing all of the parameters.

This is the page I am trying to login to: https://www.lectio.dk/lectio/11/login.aspx?lecafdeling=4733693054

having looked into to some of forms being submitted this was something i could find:

//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
}
}
//]]>

But I don't how to parse the proper value for these

Upvotes: 1

Views: 143

Answers (2)

OJunker
OJunker

Reputation: 62

I had to use

String EVENTVARGUMENTvalue = doc.select("input[name=__EVENTARGUMENT").attr("value");

Upvotes: 0

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

The easier way to see what is being sent by the browser is to inspect the raw http request and just copy the headers. You can do this with Chrome by opening the Development tools by pressing F12. Select Network. There is a bullet on the upper left. Hover it and it will display Record Network Log. This must be red. If it's not press it. This will record all the traffic of the request you will send. You will a number of rows on the table below. Sort by Method and select the row with value POST(Press on the value of the column Name. This is the actual request you are sending. On the panel on the right select Headers. Check the Request Headers and Form Data. Check if all of these values are the same. This will help you in order to hardcode some values that you are missing.

Upvotes: 1

Related Questions