简体   繁体   English

如何在Java中提交具有隐藏的具有生成值的csrf_token_login字段的html登录表单

[英]How in Java to submit html logon form that has hidden csrf_token_login field with generated value

I need to automate file downloading from a website. 我需要自动从网站下载文件。 The file download button appears only after login for which I was provided username and password. 只有提供了用户名和密码的登录后,文件下载按钮才会出现。 In login form there are two more hidden fields one of which is csrf_token_login with a generated value: 在登录表单中,还有两个隐藏字段,其中一个是具有生成值的csrf_token_login:

<input type="hidden" name="csrf_token_login" value="nl9YERDFpecfITb8QwFWneoaefykxp2b" />

It is clear how to code this in Java (using java.net.HttpUrlConnection) if I would have just login and password (there is excellent explanation for this in Using java.net.URLConnection to fire and handle HTTP requests ): submit POST request, get cookies and set them for any subsequent request. 如果我只有登录名和密码( 使用Java.net.URLConnection触发并处理HTTP请求对此有很好的解释),那么很清楚如何在Java中使用Java(使用java.net.HttpUrlConnection)编写此代码:提交POST请求,获取cookie并将其设置为任何后续请求。 But how can I get a generated value of csrf_token_login on the login form and submit it with other values? 但是,如何在登录表单上获取csrf_token_login的生成值并将其与其他值一起提交?

Reading it using getInputStream() on the HttpURLConnection of a login page gives me the csrf value. 在登录页面的HttpURLConnection上使用getInputStream()读取它会为我提供csrf值。 But at the same time this establishes connection and prevents from setting connection properties for posting data: 但是同时这会建立连接并阻止设置用于发布数据的连接属性:

private HttpURLConnection logUrlCon;
... 
BufferedReader logInput = new BufferedReader(new InputStreamReader(logUrlCon.getInputStream())); 
... // read and get csrf value OK

logUrlCon.setDoOutput(true); // throws java.lang.IllegalStateException: Already connected

Is there any way of getting this csrf_tiken_login value generated in a login form AND posting it with username and password? 有什么办法可以在登录表单中生成此csrf_tiken_login值并将其与用户名和密码一起发布?

Read login page content and extract the data using regular expressions. 读取登录页面内容并使用正则表达式提取数据。 Your hidden field has a very distinctive form (with a unique name, etc.), so perfectly suitable for regular expression based data extraction. 您的隐藏字段具有非常独特的形式(具有唯一的名称等),因此非常适合基于正则表达式的数据提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM