Pregunta

I'm pretty new/bad with regex-patterns, but this is what I want:

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?

I did make the HttpGet already (see code below), I just need to know the correct Pattern for this.

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;
    }
    catch(Exception ex){
        ex.printStackTrace();
    }

    // Get the value of the hidden input-field with the name __RequestVerificationToken
    Pattern pattern = Pattern.compile("<input name=\"" + TOKEN + "\" type=\"hidden\" value=\".\" />", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(response);
    while(matcher.find())
        hidden_token = matcher.group();

    return hidden_token;
}

So, what should I replace the following line with?

Pattern pattern = Pattern.compile("<input name=\"" + TOKEN + "\" type=\"hidden\" value=\".\" />", Pattern.DOTALL);

Or should I also change something else?

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.


Edit 1:

After reading the answer of this stackoverflow question I think it's better to not use a regex-pattern for the HTML page. Does anyone know a better solution (I would appreciate it if this better solution would be with a code sample).


Edit 2:

I tried using Illegal Argument's answer and added the Jsoup library. I did indeed manage to get the token by making the following changes to my code above:

Replace everything in the try { ... } with:

// Get the value of the hidden input-field with the name __RequestVerificationToken
Document doc = Jsoup.connect(url).get();
org.jsoup.nodes.Element el = doc.select("input[name*=" + TOKEN).first();
hidden_token = el.attr("value");

This does indeed get me the token of the hidden field, but now I have an entire new problem.. The token changed, because Jsoup opens a new session. So basically I can't use the Jsoup and are "forced" to use the already open DefaultHttpClient that I also use for the POST.

I will make a new question for this though, since my original answer was just bad questioning by myself (not providing all the details) and so I accept Illegal Argument's answer as the correct one (though it didn't solved my current problem, it might help others).

¿Fue útil?

Solución

Try using Jsoup library. Its is a regex parser built for this purpose.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top