Java Jsoup,在执行表单POST之前获取值
以下是我用来提交表单的代码:Java Jsoup,在执行表单POST之前获取值,java,forms,jsoup,httpclient,Java,Forms,Jsoup,Httpclient,以下是我用来提交表单的代码: Connection.Response res = Jsoup.connect("http://example.com") .data("id", "myID") .data("username", "myUsername") .data("code", "MyAuthcode") // get the value of Auth code from page element .method(Method.POST).execute(
Connection.Response res = Jsoup.connect("http://example.com")
.data("id", "myID")
.data("username", "myUsername")
.data("code", "MyAuthcode") // get the value of Auth code from page element
.method(Method.POST).execute();
要成功提交给定表单,需要设置带有[name=“code”]的字段的值
该值可在页面上的另一个元素中找到。在实际提交如上所示的表单之前,如何使用相同的连接获取元素的值
我需要使用元素中的值来成功填写表单。Jsoup实际上会为每个请求打开一个新的HTTP连接,因此您的请求不太可能实现,但您可以接近:
// Define where to connect (doesn't actually connect)
Connection connection = Jsoup.connect("http://example.com");
// Connect to the server and get the page
Document doc = connection.get();
// Extract the value from the page
String authCode = doc.select("input[name='code']").val();
// Add the required data to the request
connection.data("id", "myID")
.data("username", "myUsername")
.data("code", authCode);
// Connect to the server and do a post
Connection.Response response = connection.method(Method.POST).execute();
这将产生两个HTTP请求(GET和POST各一个),每个请求都有自己的连接
如果您真的只需要一个连接,那么您必须使用不同的工具来连接到服务器(例如)。您仍然可以使用jsoup来解析使用
jsoup.parse()
返回的内容。您需要从get调用中获取cookies,并将它们添加到后续的POST调用中,以便维护会话。看看这篇文章中的解决方案真的,没有人得到任何东西吗?但是每个连接的值都不同:/因此,在连接上使用第二个连接值,post将不起作用。除了Jsoup,还有其他方法可以在Java中做到这一点吗?从页面获取数据,然后发布表单,这是不可能的……看起来我将不得不使用HTTPClient。谢谢