Question

I already asked a similar question before, but wasn't providing enough details. So here is the "same" question, but this time more in-depth.

I've got a webpage with html, and somewhere on that page I have: <input name="__RequestVerificationToken" type="hidden" value="the_value_I_want" />

So, my question is: How can I get the value (the_value_I_want) of the hidden text field in Android, using HttpGet of an already open DefaultHttpClient connection?

My current code:

// Method to get the hidden-input value of the Token
private String getToken(){
    String url = "http://myhost/Account/Login";
    String hidden_token = "";
    String response = "";

    HttpGet get = new HttpGet(url);
    try{
        // Send the GET-request
        HttpResponse execute = MainActivity.HttpClient.execute(get);

        // Get the response of the GET-request
        InputStream content = execute.getEntity().getContent();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
        String s = "";
        while((s = buffer.readLine()) != null)
            response += s;

        content.close();
        buffer.close();

        // Get the value of the hidden input-field with the name __RequestVerificationToken
        // TODO
    }
    catch(Exception ex){
        ex.printStackTrace();
    }

    return hidden_token;
}

So, what should I add on the TODO-line?

Because the Token and Cookie only remain as long as the Session stays open, I can't use the Jsoup library for finding the hidden field (which I did by using the code below). Instead I need to use the already open DefaultHttpClient.

Jsoup code:

Document doc = Jsoup.connect(url).get(); // <- this opens a new session
org.jsoup.nodes.Element el = doc.select("input[name*=" + TOKEN).first();
hidden_token = el.attr("value");

Thanks in advance for the responses.

PS: For those wondering: I need this token to be able to Log-in using a Google-account with a POST-request, combined with the token I got from a Cookie.

Was it helpful?

Solution

Ok, lucky for me this was very simple. I just replaced Document doc = Jsoup.connect(url).get(); with Document doc = Jsoup.parse(html);

So in the code of the main post I replaced //TODO with:

Document doc = Jsoup.parse(response);
org.jsoup.nodes.Element el = doc.select("input[name*=" + TOKEN).first();
hidden_token = el.attr("value");

Edit 1:

I thought this did the trick, but it doesn't.. It still thinks there are two different Sessions opened.. :S

Does Jsoup.parse(...) open a new Jsoup.get-session behind the scenes?


Edit 2:

It's even worse.. Every time another page is opened on the website, another session is created and therefore another token is needed.. So I need to discuss some things with the creator of the website/web api hybrid and figure some things out.. Perhaps create a different log-in just for the Web API..

All in all I'm kinda frustrated right now, even though all the problems I've encountered are "solved"..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top