问题描述
我是pretty新/坏的正则表达式-模式
,但是这就是我想要的:
I'm pretty new/bad with regex-patterns
, but this is what I want:
我已经得到了与HTML网页,以及地方,页面我有:<输入名称=__ RequestVerificationToken类型=隐藏值=the_value_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" />
所以,我的问题是:我如何才能获得价值(the_value_I_want)的隐藏文本字段
在的Android
So, my question is: How can I get the value (the_value_I_want) of the hidden text field
in Android?
我没有让 HTTPGET
已经(见code以下),我只需要知道正确的模式
这一点。
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.compile(&LT;输入名字= \\+ TOKEN +\\型= \\隐藏\\价值= \\ /&GT\\;, Pattern.DOTALL);
或者我应该也会改变别的东西?
Or should I also change something else?
在此先感谢您的答复。
PS:对于那些想知道:我需要这个令牌能够登录,使用具有一个谷歌账户在 POST请求
,用<$ C合并$ C>标记从我得到了一个饼干
。
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
.
编辑1:
阅读的答案this计算器问题我认为这是最好不要使用正则表达式图案的HTML页面。有谁知道一个更好的解决方案(我想AP preciate它,如果这更好的解决办法是用code样品)。
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).
编辑2:
我试图使用非法参数的答案,并增加了Jsoup库。我确实管理通过了如下修改我的code以上获得令牌:
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");
这确实让我隐藏字段的象征,但现在我有一个全新的问题..令牌改变,因为Jsoup打开一个新的会话。所以基本上我不能使用Jsoup和被迫使用已打开DefaultHttpClient,我也使用了POST。
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).
推荐答案
请尝试使用库。它就是为了这个目的建立一个正则表达式解析器。
Try using Jsoup library. Its is a regex parser built for this purpose.
这篇关于Android的 - 正则表达式,模式的隐藏的HTML输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!